Complex classes like UnitOfWork often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UnitOfWork, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 67 | class UnitOfWork implements PropertyChangedListener |
||
| 68 | { |
||
| 69 | /** |
||
| 70 | * An entity is in MANAGED state when its persistence is managed by an EntityManager. |
||
| 71 | */ |
||
| 72 | const STATE_MANAGED = 1; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * An entity is new if it has just been instantiated (i.e. using the "new" operator) |
||
| 76 | * and is not (yet) managed by an EntityManager. |
||
| 77 | */ |
||
| 78 | const STATE_NEW = 2; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * A detached entity is an instance with persistent state and identity that is not |
||
| 82 | * (or no longer) associated with an EntityManager (and a UnitOfWork). |
||
| 83 | */ |
||
| 84 | const STATE_DETACHED = 3; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * A removed entity instance is an instance with a persistent identity, |
||
| 88 | * associated with an EntityManager, whose persistent state will be deleted |
||
| 89 | * on commit. |
||
| 90 | */ |
||
| 91 | const STATE_REMOVED = 4; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Hint used to collect all primary keys of associated entities during hydration |
||
| 95 | * and execute it in a dedicated query afterwards |
||
| 96 | * @see https://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html?highlight=eager#temporarily-change-fetch-mode-in-dql |
||
| 97 | */ |
||
| 98 | const HINT_DEFEREAGERLOAD = 'deferEagerLoad'; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The identity map that holds references to all managed entities that have |
||
| 102 | * an identity. The entities are grouped by their class name. |
||
| 103 | * Since all classes in a hierarchy must share the same identifier set, |
||
| 104 | * we always take the root class name of the hierarchy. |
||
| 105 | * |
||
| 106 | * @var array |
||
| 107 | */ |
||
| 108 | private $identityMap = array(); |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Map of all identifiers of managed entities. |
||
| 112 | * Keys are object ids (spl_object_hash). |
||
| 113 | * |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | private $entityIdentifiers = array(); |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Map of the original entity data of managed entities. |
||
| 120 | * Keys are object ids (spl_object_hash). This is used for calculating changesets |
||
| 121 | * at commit time. |
||
| 122 | * |
||
| 123 | * Internal note: Note that PHPs "copy-on-write" behavior helps a lot with memory usage. |
||
| 124 | * A value will only really be copied if the value in the entity is modified |
||
| 125 | * by the user. |
||
| 126 | * |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | private $originalEntityData = array(); |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Map of entity changes. Keys are object ids (spl_object_hash). |
||
| 133 | * Filled at the beginning of a commit of the UnitOfWork and cleaned at the end. |
||
| 134 | * |
||
| 135 | * @var array |
||
| 136 | */ |
||
| 137 | private $entityChangeSets = array(); |
||
| 138 | |||
| 139 | /** |
||
| 140 | * The (cached) states of any known entities. |
||
| 141 | * Keys are object ids (spl_object_hash). |
||
| 142 | * |
||
| 143 | * @var array |
||
| 144 | */ |
||
| 145 | private $entityStates = array(); |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Map of entities that are scheduled for dirty checking at commit time. |
||
| 149 | * This is only used for entities with a change tracking policy of DEFERRED_EXPLICIT. |
||
| 150 | * Keys are object ids (spl_object_hash). |
||
| 151 | * |
||
| 152 | * @var array |
||
| 153 | */ |
||
| 154 | private $scheduledForSynchronization = array(); |
||
| 155 | |||
| 156 | /** |
||
| 157 | * A list of all pending entity insertions. |
||
| 158 | * |
||
| 159 | * @var array |
||
| 160 | */ |
||
| 161 | private $entityInsertions = array(); |
||
| 162 | |||
| 163 | /** |
||
| 164 | * A list of all pending entity updates. |
||
| 165 | * |
||
| 166 | * @var array |
||
| 167 | */ |
||
| 168 | private $entityUpdates = array(); |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Any pending extra updates that have been scheduled by persisters. |
||
| 172 | * |
||
| 173 | * @var array |
||
| 174 | */ |
||
| 175 | private $extraUpdates = array(); |
||
| 176 | |||
| 177 | /** |
||
| 178 | * A list of all pending entity deletions. |
||
| 179 | * |
||
| 180 | * @var array |
||
| 181 | */ |
||
| 182 | private $entityDeletions = array(); |
||
| 183 | |||
| 184 | /** |
||
| 185 | * All pending collection deletions. |
||
| 186 | * |
||
| 187 | * @var array |
||
| 188 | */ |
||
| 189 | private $collectionDeletions = array(); |
||
| 190 | |||
| 191 | /** |
||
| 192 | * All pending collection updates. |
||
| 193 | * |
||
| 194 | * @var array |
||
| 195 | */ |
||
| 196 | private $collectionUpdates = array(); |
||
| 197 | |||
| 198 | /** |
||
| 199 | * List of collections visited during changeset calculation on a commit-phase of a UnitOfWork. |
||
| 200 | * At the end of the UnitOfWork all these collections will make new snapshots |
||
| 201 | * of their data. |
||
| 202 | * |
||
| 203 | * @var array |
||
| 204 | */ |
||
| 205 | private $visitedCollections = array(); |
||
| 206 | |||
| 207 | /** |
||
| 208 | * The EntityManager that "owns" this UnitOfWork instance. |
||
| 209 | * |
||
| 210 | * @var EntityManagerInterface |
||
| 211 | */ |
||
| 212 | private $em; |
||
| 213 | |||
| 214 | /** |
||
| 215 | * The entity persister instances used to persist entity instances. |
||
| 216 | * |
||
| 217 | * @var array |
||
| 218 | */ |
||
| 219 | private $persisters = array(); |
||
| 220 | |||
| 221 | /** |
||
| 222 | * The collection persister instances used to persist collections. |
||
| 223 | * |
||
| 224 | * @var array |
||
| 225 | */ |
||
| 226 | private $collectionPersisters = array(); |
||
| 227 | |||
| 228 | /** |
||
| 229 | * The EventManager used for dispatching events. |
||
| 230 | * |
||
| 231 | * @var \Doctrine\Common\EventManager |
||
| 232 | */ |
||
| 233 | private $evm; |
||
| 234 | |||
| 235 | /** |
||
| 236 | * The ListenersInvoker used for dispatching events. |
||
| 237 | * |
||
| 238 | * @var \Doctrine\ORM\Event\ListenersInvoker |
||
| 239 | */ |
||
| 240 | private $listenersInvoker; |
||
| 241 | |||
| 242 | /** |
||
| 243 | * The IdentifierFlattener used for manipulating identifiers |
||
| 244 | * |
||
| 245 | * @var \Doctrine\ORM\Utility\IdentifierFlattener |
||
| 246 | */ |
||
| 247 | private $identifierFlattener; |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Orphaned entities that are scheduled for removal. |
||
| 251 | * |
||
| 252 | * @var array |
||
| 253 | */ |
||
| 254 | private $orphanRemovals = array(); |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Read-Only objects are never evaluated |
||
| 258 | * |
||
| 259 | * @var array |
||
| 260 | */ |
||
| 261 | private $readOnlyObjects = array(); |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Map of Entity Class-Names and corresponding IDs that should eager loaded when requested. |
||
| 265 | * |
||
| 266 | * @var array |
||
| 267 | */ |
||
| 268 | private $eagerLoadingEntities = array(); |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @var boolean |
||
| 272 | */ |
||
| 273 | protected $hasCache = false; |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Helper for handling completion of hydration |
||
| 277 | * |
||
| 278 | * @var HydrationCompleteHandler |
||
| 279 | */ |
||
| 280 | private $hydrationCompleteHandler; |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @var ReflectionPropertiesGetter |
||
| 284 | */ |
||
| 285 | private $reflectionPropertiesGetter; |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Initializes a new UnitOfWork instance, bound to the given EntityManager. |
||
| 289 | * |
||
| 290 | * @param EntityManagerInterface $em |
||
| 291 | */ |
||
| 292 | 2351 | public function __construct(EntityManagerInterface $em) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Commits the UnitOfWork, executing all operations that have been postponed |
||
| 305 | * up to this point. The state of all managed entities will be synchronized with |
||
| 306 | * the database. |
||
| 307 | * |
||
| 308 | * The operations are executed in the following order: |
||
| 309 | * |
||
| 310 | * 1) All entity insertions |
||
| 311 | * 2) All entity updates |
||
| 312 | * 3) All collection deletions |
||
| 313 | * 4) All collection updates |
||
| 314 | * 5) All entity deletions |
||
| 315 | * |
||
| 316 | * @param null|object|array $entity |
||
| 317 | * |
||
| 318 | * @return void |
||
| 319 | * |
||
| 320 | * @throws \Exception |
||
| 321 | */ |
||
| 322 | 1022 | public function commit($entity = null) |
|
| 323 | { |
||
| 324 | // Raise preFlush |
||
| 325 | 1022 | if ($this->evm->hasListeners(Events::preFlush)) { |
|
| 326 | 2 | $this->evm->dispatchEvent(Events::preFlush, new PreFlushEventArgs($this->em)); |
|
| 327 | } |
||
| 328 | |||
| 329 | // Compute changes done since last commit. |
||
| 330 | 1022 | if ($entity === null) { |
|
| 331 | 1014 | $this->computeChangeSets(); |
|
| 332 | 16 | } elseif (is_object($entity)) { |
|
| 333 | 15 | $this->computeSingleEntityChangeSet($entity); |
|
| 334 | 1 | } elseif (is_array($entity)) { |
|
| 335 | 1 | foreach ($entity as $object) { |
|
| 336 | 1 | $this->computeSingleEntityChangeSet($object); |
|
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | 1019 | if ( ! ($this->entityInsertions || |
|
|
|
|||
| 341 | 166 | $this->entityDeletions || |
|
| 342 | 130 | $this->entityUpdates || |
|
| 343 | 40 | $this->collectionUpdates || |
|
| 344 | 37 | $this->collectionDeletions || |
|
| 345 | 1019 | $this->orphanRemovals)) { |
|
| 346 | 25 | $this->dispatchOnFlushEvent(); |
|
| 347 | 25 | $this->dispatchPostFlushEvent(); |
|
| 348 | |||
| 349 | 25 | return; // Nothing to do. |
|
| 350 | } |
||
| 351 | |||
| 352 | 1015 | if ($this->orphanRemovals) { |
|
| 353 | 16 | foreach ($this->orphanRemovals as $orphan) { |
|
| 354 | 16 | $this->remove($orphan); |
|
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | 1015 | $this->dispatchOnFlushEvent(); |
|
| 359 | |||
| 360 | // Now we need a commit order to maintain referential integrity |
||
| 361 | 1015 | $commitOrder = $this->getCommitOrder(); |
|
| 362 | |||
| 363 | 1015 | $conn = $this->em->getConnection(); |
|
| 364 | 1015 | $conn->beginTransaction(); |
|
| 365 | |||
| 366 | try { |
||
| 367 | // Collection deletions (deletions of complete collections) |
||
| 368 | 1015 | foreach ($this->collectionDeletions as $collectionToDelete) { |
|
| 369 | 19 | $this->getCollectionPersister($collectionToDelete->getMapping())->delete($collectionToDelete); |
|
| 370 | } |
||
| 371 | |||
| 372 | 1015 | if ($this->entityInsertions) { |
|
| 373 | 1011 | foreach ($commitOrder as $class) { |
|
| 374 | 1011 | $this->executeInserts($class); |
|
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | 1014 | if ($this->entityUpdates) { |
|
| 379 | 116 | foreach ($commitOrder as $class) { |
|
| 380 | 116 | $this->executeUpdates($class); |
|
| 381 | } |
||
| 382 | } |
||
| 383 | |||
| 384 | // Extra updates that were requested by persisters. |
||
| 385 | 1010 | if ($this->extraUpdates) { |
|
| 386 | 40 | $this->executeExtraUpdates(); |
|
| 387 | } |
||
| 388 | |||
| 389 | // Collection updates (deleteRows, updateRows, insertRows) |
||
| 390 | 1010 | foreach ($this->collectionUpdates as $collectionToUpdate) { |
|
| 391 | 533 | $this->getCollectionPersister($collectionToUpdate->getMapping())->update($collectionToUpdate); |
|
| 392 | } |
||
| 393 | |||
| 394 | // Entity deletions come last and need to be in reverse commit order |
||
| 395 | 1010 | if ($this->entityDeletions) { |
|
| 396 | 63 | for ($count = count($commitOrder), $i = $count - 1; $i >= 0 && $this->entityDeletions; --$i) { |
|
| 397 | 63 | $this->executeDeletions($commitOrder[$i]); |
|
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 401 | 1010 | $conn->commit(); |
|
| 402 | 11 | } catch (Exception $e) { |
|
| 403 | 11 | $this->em->close(); |
|
| 404 | 11 | $conn->rollBack(); |
|
| 405 | |||
| 406 | 11 | $this->afterTransactionRolledBack(); |
|
| 407 | |||
| 408 | 11 | throw $e; |
|
| 409 | } |
||
| 410 | |||
| 411 | 1010 | $this->afterTransactionComplete(); |
|
| 412 | |||
| 413 | // Take new snapshots from visited collections |
||
| 414 | 1010 | foreach ($this->visitedCollections as $coll) { |
|
| 415 | 532 | $coll->takeSnapshot(); |
|
| 416 | } |
||
| 417 | |||
| 418 | 1010 | $this->dispatchPostFlushEvent(); |
|
| 419 | |||
| 420 | // Clear up |
||
| 421 | 1009 | $this->entityInsertions = |
|
| 422 | 1009 | $this->entityUpdates = |
|
| 423 | 1009 | $this->entityDeletions = |
|
| 424 | 1009 | $this->extraUpdates = |
|
| 425 | 1009 | $this->entityChangeSets = |
|
| 426 | 1009 | $this->collectionUpdates = |
|
| 427 | 1009 | $this->collectionDeletions = |
|
| 428 | 1009 | $this->visitedCollections = |
|
| 429 | 1009 | $this->scheduledForSynchronization = |
|
| 430 | 1009 | $this->orphanRemovals = array(); |
|
| 431 | 1009 | } |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Computes the changesets of all entities scheduled for insertion. |
||
| 435 | * |
||
| 436 | * @return void |
||
| 437 | */ |
||
| 438 | 1021 | private function computeScheduleInsertsChangeSets() |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Only flushes the given entity according to a ruleset that keeps the UoW consistent. |
||
| 449 | * |
||
| 450 | * 1. All entities scheduled for insertion, (orphan) removals and changes in collections are processed as well! |
||
| 451 | * 2. Read Only entities are skipped. |
||
| 452 | * 3. Proxies are skipped. |
||
| 453 | * 4. Only if entity is properly managed. |
||
| 454 | * |
||
| 455 | * @param object $entity |
||
| 456 | * |
||
| 457 | * @return void |
||
| 458 | * |
||
| 459 | * @throws \InvalidArgumentException |
||
| 460 | */ |
||
| 461 | 16 | private function computeSingleEntityChangeSet($entity) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Executes any extra updates that have been scheduled. |
||
| 497 | */ |
||
| 498 | 40 | private function executeExtraUpdates() |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Gets the changeset for an entity. |
||
| 512 | * |
||
| 513 | * @param object $entity |
||
| 514 | * |
||
| 515 | * @return array |
||
| 516 | */ |
||
| 517 | 1013 | public function & getEntityChangeSet($entity) |
|
| 518 | { |
||
| 519 | 1013 | $oid = spl_object_hash($entity); |
|
| 520 | 1013 | $data = array(); |
|
| 521 | |||
| 522 | 1013 | if (!isset($this->entityChangeSets[$oid])) { |
|
| 523 | 1 | return $data; |
|
| 524 | } |
||
| 525 | |||
| 526 | 1013 | return $this->entityChangeSets[$oid]; |
|
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Computes the changes that happened to a single entity. |
||
| 531 | * |
||
| 532 | * Modifies/populates the following properties: |
||
| 533 | * |
||
| 534 | * {@link _originalEntityData} |
||
| 535 | * If the entity is NEW or MANAGED but not yet fully persisted (only has an id) |
||
| 536 | * then it was not fetched from the database and therefore we have no original |
||
| 537 | * entity data yet. All of the current entity data is stored as the original entity data. |
||
| 538 | * |
||
| 539 | * {@link _entityChangeSets} |
||
| 540 | * The changes detected on all properties of the entity are stored there. |
||
| 541 | * A change is a tuple array where the first entry is the old value and the second |
||
| 542 | * entry is the new value of the property. Changesets are used by persisters |
||
| 543 | * to INSERT/UPDATE the persistent entity state. |
||
| 544 | * |
||
| 545 | * {@link _entityUpdates} |
||
| 546 | * If the entity is already fully MANAGED (has been fetched from the database before) |
||
| 547 | * and any changes to its properties are detected, then a reference to the entity is stored |
||
| 548 | * there to mark it for an update. |
||
| 549 | * |
||
| 550 | * {@link _collectionDeletions} |
||
| 551 | * If a PersistentCollection has been de-referenced in a fully MANAGED entity, |
||
| 552 | * then this collection is marked for deletion. |
||
| 553 | * |
||
| 554 | * @ignore |
||
| 555 | * |
||
| 556 | * @internal Don't call from the outside. |
||
| 557 | * |
||
| 558 | * @param ClassMetadata $class The class descriptor of the entity. |
||
| 559 | * @param object $entity The entity for which to compute the changes. |
||
| 560 | * |
||
| 561 | * @return void |
||
| 562 | */ |
||
| 563 | 1023 | public function computeChangeSet(ClassMetadata $class, $entity) |
|
| 564 | { |
||
| 565 | 1023 | $oid = spl_object_hash($entity); |
|
| 566 | |||
| 567 | 1023 | if (isset($this->readOnlyObjects[$oid])) { |
|
| 568 | 2 | return; |
|
| 569 | } |
||
| 570 | |||
| 571 | 1023 | if ( ! $class->isInheritanceTypeNone()) { |
|
| 572 | 311 | $class = $this->em->getClassMetadata(get_class($entity)); |
|
| 573 | } |
||
| 574 | |||
| 575 | 1023 | $invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::preFlush) & ~ListenersInvoker::INVOKE_MANAGER; |
|
| 576 | |||
| 577 | 1023 | if ($invoke !== ListenersInvoker::INVOKE_NONE) { |
|
| 578 | 137 | $this->listenersInvoker->invoke($class, Events::preFlush, $entity, new PreFlushEventArgs($this->em), $invoke); |
|
| 579 | } |
||
| 580 | |||
| 581 | 1023 | $actualData = array(); |
|
| 582 | |||
| 583 | 1023 | foreach ($class->reflFields as $name => $refProp) { |
|
| 584 | 1023 | $value = $refProp->getValue($entity); |
|
| 585 | |||
| 586 | 1023 | if ($class->isCollectionValuedAssociation($name) && $value !== null) { |
|
| 587 | 781 | if ($value instanceof PersistentCollection) { |
|
| 588 | 201 | if ($value->getOwner() === $entity) { |
|
| 589 | 201 | continue; |
|
| 590 | } |
||
| 591 | |||
| 592 | 5 | $value = new ArrayCollection($value->getValues()); |
|
| 593 | } |
||
| 594 | |||
| 595 | // If $value is not a Collection then use an ArrayCollection. |
||
| 596 | 776 | if ( ! $value instanceof Collection) { |
|
| 597 | 242 | $value = new ArrayCollection($value); |
|
| 598 | } |
||
| 599 | |||
| 600 | 776 | $assoc = $class->associationMappings[$name]; |
|
| 601 | |||
| 602 | // Inject PersistentCollection |
||
| 603 | 776 | $value = new PersistentCollection( |
|
| 604 | 776 | $this->em, $this->em->getClassMetadata($assoc['targetEntity']), $value |
|
| 605 | ); |
||
| 606 | 776 | $value->setOwner($entity, $assoc); |
|
| 607 | 776 | $value->setDirty( ! $value->isEmpty()); |
|
| 608 | |||
| 609 | 776 | $class->reflFields[$name]->setValue($entity, $value); |
|
| 610 | |||
| 611 | 776 | $actualData[$name] = $value; |
|
| 612 | |||
| 613 | 776 | continue; |
|
| 614 | } |
||
| 615 | |||
| 616 | 1023 | if (( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) && ($name !== $class->versionField)) { |
|
| 617 | 1023 | $actualData[$name] = $value; |
|
| 618 | } |
||
| 619 | } |
||
| 620 | |||
| 621 | 1023 | if ( ! isset($this->originalEntityData[$oid])) { |
|
| 622 | // Entity is either NEW or MANAGED but not yet fully persisted (only has an id). |
||
| 623 | // These result in an INSERT. |
||
| 624 | 1019 | $this->originalEntityData[$oid] = $actualData; |
|
| 625 | 1019 | $changeSet = array(); |
|
| 626 | |||
| 627 | 1019 | foreach ($actualData as $propName => $actualValue) { |
|
| 628 | 1004 | if ( ! isset($class->associationMappings[$propName])) { |
|
| 629 | 953 | $changeSet[$propName] = array(null, $actualValue); |
|
| 630 | |||
| 631 | 953 | continue; |
|
| 632 | } |
||
| 633 | |||
| 634 | 898 | $assoc = $class->associationMappings[$propName]; |
|
| 635 | |||
| 636 | 898 | if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) { |
|
| 637 | 898 | $changeSet[$propName] = array(null, $actualValue); |
|
| 638 | } |
||
| 639 | } |
||
| 640 | |||
| 641 | 1019 | $this->entityChangeSets[$oid] = $changeSet; |
|
| 642 | } else { |
||
| 643 | // Entity is "fully" MANAGED: it was already fully persisted before |
||
| 644 | // and we have a copy of the original data |
||
| 645 | 266 | $originalData = $this->originalEntityData[$oid]; |
|
| 646 | 266 | $isChangeTrackingNotify = $class->isChangeTrackingNotify(); |
|
| 647 | 266 | $changeSet = ($isChangeTrackingNotify && isset($this->entityChangeSets[$oid])) |
|
| 648 | ? $this->entityChangeSets[$oid] |
||
| 649 | 266 | : array(); |
|
| 650 | |||
| 651 | 266 | foreach ($actualData as $propName => $actualValue) { |
|
| 652 | // skip field, its a partially omitted one! |
||
| 653 | 251 | if ( ! (isset($originalData[$propName]) || array_key_exists($propName, $originalData))) { |
|
| 654 | 8 | continue; |
|
| 655 | } |
||
| 656 | |||
| 657 | 251 | $orgValue = $originalData[$propName]; |
|
| 658 | |||
| 659 | // skip if value haven't changed |
||
| 660 | 251 | if ($orgValue === $actualValue) { |
|
| 661 | 235 | continue; |
|
| 662 | } |
||
| 663 | |||
| 664 | // if regular field |
||
| 665 | 112 | if ( ! isset($class->associationMappings[$propName])) { |
|
| 666 | 58 | if ($isChangeTrackingNotify) { |
|
| 667 | continue; |
||
| 668 | } |
||
| 669 | |||
| 670 | 58 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 671 | |||
| 672 | 58 | continue; |
|
| 673 | } |
||
| 674 | |||
| 675 | 58 | $assoc = $class->associationMappings[$propName]; |
|
| 676 | |||
| 677 | // Persistent collection was exchanged with the "originally" |
||
| 678 | // created one. This can only mean it was cloned and replaced |
||
| 679 | // on another entity. |
||
| 680 | 58 | if ($actualValue instanceof PersistentCollection) { |
|
| 681 | 8 | $owner = $actualValue->getOwner(); |
|
| 682 | 8 | if ($owner === null) { // cloned |
|
| 683 | $actualValue->setOwner($entity, $assoc); |
||
| 684 | 8 | } else if ($owner !== $entity) { // no clone, we have to fix |
|
| 685 | if (!$actualValue->isInitialized()) { |
||
| 686 | $actualValue->initialize(); // we have to do this otherwise the cols share state |
||
| 687 | } |
||
| 688 | $newValue = clone $actualValue; |
||
| 689 | $newValue->setOwner($entity, $assoc); |
||
| 690 | $class->reflFields[$propName]->setValue($entity, $newValue); |
||
| 691 | } |
||
| 692 | } |
||
| 693 | |||
| 694 | 58 | if ($orgValue instanceof PersistentCollection) { |
|
| 695 | // A PersistentCollection was de-referenced, so delete it. |
||
| 696 | 8 | $coid = spl_object_hash($orgValue); |
|
| 697 | |||
| 698 | 8 | if (isset($this->collectionDeletions[$coid])) { |
|
| 699 | continue; |
||
| 700 | } |
||
| 701 | |||
| 702 | 8 | $this->collectionDeletions[$coid] = $orgValue; |
|
| 703 | 8 | $changeSet[$propName] = $orgValue; // Signal changeset, to-many assocs will be ignored. |
|
| 704 | |||
| 705 | 8 | continue; |
|
| 706 | } |
||
| 707 | |||
| 708 | 50 | if ($assoc['type'] & ClassMetadata::TO_ONE) { |
|
| 709 | 49 | if ($assoc['isOwningSide']) { |
|
| 710 | 21 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 711 | } |
||
| 712 | |||
| 713 | 49 | if ($orgValue !== null && $assoc['orphanRemoval']) { |
|
| 714 | 50 | $this->scheduleOrphanRemoval($orgValue); |
|
| 715 | } |
||
| 716 | } |
||
| 717 | } |
||
| 718 | |||
| 719 | 266 | if ($changeSet) { |
|
| 720 | 85 | $this->entityChangeSets[$oid] = $changeSet; |
|
| 721 | 85 | $this->originalEntityData[$oid] = $actualData; |
|
| 722 | 85 | $this->entityUpdates[$oid] = $entity; |
|
| 723 | } |
||
| 724 | } |
||
| 725 | |||
| 726 | // Look for changes in associations of the entity |
||
| 727 | 1023 | foreach ($class->associationMappings as $field => $assoc) { |
|
| 728 | 898 | if (($val = $class->reflFields[$field]->getValue($entity)) === null) { |
|
| 729 | 640 | continue; |
|
| 730 | } |
||
| 731 | |||
| 732 | 869 | $this->computeAssociationChanges($assoc, $val); |
|
| 733 | |||
| 734 | 861 | if ( ! isset($this->entityChangeSets[$oid]) && |
|
| 735 | 861 | $assoc['isOwningSide'] && |
|
| 736 | 861 | $assoc['type'] == ClassMetadata::MANY_TO_MANY && |
|
| 737 | 861 | $val instanceof PersistentCollection && |
|
| 738 | 861 | $val->isDirty()) { |
|
| 739 | |||
| 740 | 35 | $this->entityChangeSets[$oid] = array(); |
|
| 741 | 35 | $this->originalEntityData[$oid] = $actualData; |
|
| 742 | 861 | $this->entityUpdates[$oid] = $entity; |
|
| 743 | } |
||
| 744 | } |
||
| 745 | 1015 | } |
|
| 746 | |||
| 747 | /** |
||
| 748 | * Computes all the changes that have been done to entities and collections |
||
| 749 | * since the last commit and stores these changes in the _entityChangeSet map |
||
| 750 | * temporarily for access by the persisters, until the UoW commit is finished. |
||
| 751 | * |
||
| 752 | * @return void |
||
| 753 | */ |
||
| 754 | 1014 | public function computeChangeSets() |
|
| 799 | |||
| 800 | /** |
||
| 801 | * Computes the changes of an association. |
||
| 802 | * |
||
| 803 | * @param array $assoc The association mapping. |
||
| 804 | * @param mixed $value The value of the association. |
||
| 805 | * |
||
| 806 | * @throws ORMInvalidArgumentException |
||
| 807 | * @throws ORMException |
||
| 808 | * |
||
| 809 | * @return void |
||
| 810 | */ |
||
| 811 | 869 | private function computeAssociationChanges($assoc, $value) |
|
| 812 | { |
||
| 813 | 869 | if ($value instanceof Proxy && ! $value->__isInitialized__) { |
|
| 814 | 29 | return; |
|
| 815 | } |
||
| 816 | |||
| 817 | 868 | if ($value instanceof PersistentCollection && $value->isDirty()) { |
|
| 818 | 535 | $coid = spl_object_hash($value); |
|
| 819 | |||
| 820 | 535 | $this->collectionUpdates[$coid] = $value; |
|
| 821 | 535 | $this->visitedCollections[$coid] = $value; |
|
| 822 | } |
||
| 823 | |||
| 824 | // Look through the entities, and in any of their associations, |
||
| 825 | // for transient (new) entities, recursively. ("Persistence by reachability") |
||
| 826 | // Unwrap. Uninitialized collections will simply be empty. |
||
| 827 | 868 | $unwrappedValue = ($assoc['type'] & ClassMetadata::TO_ONE) ? array($value) : $value->unwrap(); |
|
| 828 | 868 | $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); |
|
| 829 | |||
| 830 | 868 | foreach ($unwrappedValue as $key => $entry) { |
|
| 831 | 724 | if (! ($entry instanceof $targetClass->name)) { |
|
| 832 | 6 | throw ORMInvalidArgumentException::invalidAssociation($targetClass, $assoc, $entry); |
|
| 833 | } |
||
| 834 | |||
| 835 | 718 | $state = $this->getEntityState($entry, self::STATE_NEW); |
|
| 836 | |||
| 837 | 718 | if ( ! ($entry instanceof $assoc['targetEntity'])) { |
|
| 838 | throw ORMException::unexpectedAssociationValue($assoc['sourceEntity'], $assoc['fieldName'], get_class($entry), $assoc['targetEntity']); |
||
| 839 | } |
||
| 840 | |||
| 841 | switch ($state) { |
||
| 842 | 718 | case self::STATE_NEW: |
|
| 843 | 39 | if ( ! $assoc['isCascadePersist']) { |
|
| 844 | 4 | throw ORMInvalidArgumentException::newEntityFoundThroughRelationship($assoc, $entry); |
|
| 845 | } |
||
| 846 | |||
| 847 | 35 | $this->persistNew($targetClass, $entry); |
|
| 848 | 35 | $this->computeChangeSet($targetClass, $entry); |
|
| 849 | 35 | break; |
|
| 850 | |||
| 851 | 712 | case self::STATE_REMOVED: |
|
| 852 | // Consume the $value as array (it's either an array or an ArrayAccess) |
||
| 853 | // and remove the element from Collection. |
||
| 854 | 4 | if ($assoc['type'] & ClassMetadata::TO_MANY) { |
|
| 855 | 3 | unset($value[$key]); |
|
| 856 | } |
||
| 857 | 4 | break; |
|
| 858 | |||
| 859 | 712 | case self::STATE_DETACHED: |
|
| 860 | // Can actually not happen right now as we assume STATE_NEW, |
||
| 861 | // so the exception will be raised from the DBAL layer (constraint violation). |
||
| 862 | throw ORMInvalidArgumentException::detachedEntityFoundThroughRelationship($assoc, $entry); |
||
| 863 | break; |
||
| 864 | |||
| 865 | 715 | default: |
|
| 866 | // MANAGED associated entities are already taken into account |
||
| 867 | // during changeset calculation anyway, since they are in the identity map. |
||
| 868 | } |
||
| 869 | } |
||
| 870 | 860 | } |
|
| 871 | |||
| 872 | /** |
||
| 873 | * @param \Doctrine\ORM\Mapping\ClassMetadata $class |
||
| 874 | * @param object $entity |
||
| 875 | * |
||
| 876 | * @return void |
||
| 877 | */ |
||
| 878 | 1039 | private function persistNew($class, $entity) |
|
| 905 | |||
| 906 | /** |
||
| 907 | * INTERNAL: |
||
| 908 | * Computes the changeset of an individual entity, independently of the |
||
| 909 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
||
| 910 | * |
||
| 911 | * The passed entity must be a managed entity. If the entity already has a change set |
||
| 912 | * because this method is invoked during a commit cycle then the change sets are added. |
||
| 913 | * whereby changes detected in this method prevail. |
||
| 914 | * |
||
| 915 | * @ignore |
||
| 916 | * |
||
| 917 | * @param ClassMetadata $class The class descriptor of the entity. |
||
| 918 | * @param object $entity The entity for which to (re)calculate the change set. |
||
| 919 | * |
||
| 920 | * @return void |
||
| 921 | * |
||
| 922 | * @throws ORMInvalidArgumentException If the passed entity is not MANAGED. |
||
| 923 | */ |
||
| 924 | 16 | public function recomputeSingleEntityChangeSet(ClassMetadata $class, $entity) |
|
| 976 | |||
| 977 | /** |
||
| 978 | * Executes all entity insertions for entities of the specified type. |
||
| 979 | * |
||
| 980 | * @param \Doctrine\ORM\Mapping\ClassMetadata $class |
||
| 981 | * |
||
| 982 | * @return void |
||
| 983 | */ |
||
| 984 | 1011 | private function executeInserts($class) |
|
| 1030 | |||
| 1031 | /** |
||
| 1032 | * Executes all entity updates for entities of the specified type. |
||
| 1033 | * |
||
| 1034 | * @param \Doctrine\ORM\Mapping\ClassMetadata $class |
||
| 1035 | * |
||
| 1036 | * @return void |
||
| 1037 | */ |
||
| 1038 | 116 | private function executeUpdates($class) |
|
| 1039 | { |
||
| 1040 | 116 | $className = $class->name; |
|
| 1041 | 116 | $persister = $this->getEntityPersister($className); |
|
| 1042 | 116 | $preUpdateInvoke = $this->listenersInvoker->getSubscribedSystems($class, Events::preUpdate); |
|
| 1043 | 116 | $postUpdateInvoke = $this->listenersInvoker->getSubscribedSystems($class, Events::postUpdate); |
|
| 1044 | |||
| 1045 | 116 | foreach ($this->entityUpdates as $oid => $entity) { |
|
| 1046 | 116 | if ($this->em->getClassMetadata(get_class($entity))->name !== $className) { |
|
| 1047 | 74 | continue; |
|
| 1048 | } |
||
| 1049 | |||
| 1050 | 116 | if ($preUpdateInvoke != ListenersInvoker::INVOKE_NONE) { |
|
| 1051 | 13 | $this->listenersInvoker->invoke($class, Events::preUpdate, $entity, new PreUpdateEventArgs($entity, $this->em, $this->getEntityChangeSet($entity)), $preUpdateInvoke); |
|
| 1052 | |||
| 1053 | 13 | $this->recomputeSingleEntityChangeSet($class, $entity); |
|
| 1054 | } |
||
| 1055 | |||
| 1056 | 116 | if ( ! empty($this->entityChangeSets[$oid])) { |
|
| 1057 | 82 | $persister->update($entity); |
|
| 1058 | } |
||
| 1059 | |||
| 1060 | 112 | unset($this->entityUpdates[$oid]); |
|
| 1061 | |||
| 1062 | 112 | if ($postUpdateInvoke != ListenersInvoker::INVOKE_NONE) { |
|
| 1063 | 112 | $this->listenersInvoker->invoke($class, Events::postUpdate, $entity, new LifecycleEventArgs($entity, $this->em), $postUpdateInvoke); |
|
| 1064 | } |
||
| 1065 | } |
||
| 1066 | 112 | } |
|
| 1067 | |||
| 1068 | /** |
||
| 1069 | * Executes all entity deletions for entities of the specified type. |
||
| 1070 | * |
||
| 1071 | * @param \Doctrine\ORM\Mapping\ClassMetadata $class |
||
| 1072 | * |
||
| 1073 | * @return void |
||
| 1074 | */ |
||
| 1075 | 63 | private function executeDeletions($class) |
|
| 1107 | |||
| 1108 | /** |
||
| 1109 | * Gets the commit order. |
||
| 1110 | * |
||
| 1111 | * @param array|null $entityChangeSet |
||
| 1112 | * |
||
| 1113 | * @return array |
||
| 1114 | */ |
||
| 1115 | 1015 | private function getCommitOrder(array $entityChangeSet = null) |
|
| 1116 | { |
||
| 1117 | 1015 | if ($entityChangeSet === null) { |
|
| 1118 | 1015 | $entityChangeSet = array_merge($this->entityInsertions, $this->entityUpdates, $this->entityDeletions); |
|
| 1119 | } |
||
| 1120 | |||
| 1121 | 1015 | $calc = $this->getCommitOrderCalculator(); |
|
| 1122 | |||
| 1123 | // See if there are any new classes in the changeset, that are not in the |
||
| 1124 | // commit order graph yet (don't have a node). |
||
| 1125 | // We have to inspect changeSet to be able to correctly build dependencies. |
||
| 1126 | // It is not possible to use IdentityMap here because post inserted ids |
||
| 1127 | // are not yet available. |
||
| 1128 | 1015 | $newNodes = array(); |
|
| 1129 | |||
| 1130 | 1015 | foreach ($entityChangeSet as $entity) { |
|
| 1131 | 1015 | $class = $this->em->getClassMetadata(get_class($entity)); |
|
| 1132 | |||
| 1133 | 1015 | if ($calc->hasNode($class->name)) { |
|
| 1134 | 630 | continue; |
|
| 1135 | } |
||
| 1136 | |||
| 1137 | 1015 | $calc->addNode($class->name, $class); |
|
| 1138 | |||
| 1139 | 1015 | $newNodes[] = $class; |
|
| 1140 | } |
||
| 1141 | |||
| 1142 | // Calculate dependencies for new nodes |
||
| 1143 | 1015 | while ($class = array_pop($newNodes)) { |
|
| 1144 | 1015 | foreach ($class->associationMappings as $assoc) { |
|
| 1145 | 890 | if ( ! ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE)) { |
|
| 1146 | 850 | continue; |
|
| 1147 | } |
||
| 1148 | |||
| 1149 | 843 | $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); |
|
| 1150 | |||
| 1151 | 843 | if ( ! $calc->hasNode($targetClass->name)) { |
|
| 1152 | 653 | $calc->addNode($targetClass->name, $targetClass); |
|
| 1153 | |||
| 1154 | 653 | $newNodes[] = $targetClass; |
|
| 1155 | } |
||
| 1156 | |||
| 1157 | 843 | $joinColumns = reset($assoc['joinColumns']); |
|
| 1158 | |||
| 1159 | 843 | $calc->addDependency($targetClass->name, $class->name, (int)empty($joinColumns['nullable'])); |
|
| 1160 | |||
| 1161 | // If the target class has mapped subclasses, these share the same dependency. |
||
| 1162 | 843 | if ( ! $targetClass->subClasses) { |
|
| 1163 | 836 | continue; |
|
| 1164 | } |
||
| 1165 | |||
| 1166 | 218 | foreach ($targetClass->subClasses as $subClassName) { |
|
| 1167 | 218 | $targetSubClass = $this->em->getClassMetadata($subClassName); |
|
| 1168 | |||
| 1169 | 218 | if ( ! $calc->hasNode($subClassName)) { |
|
| 1170 | 190 | $calc->addNode($targetSubClass->name, $targetSubClass); |
|
| 1171 | |||
| 1172 | 190 | $newNodes[] = $targetSubClass; |
|
| 1173 | } |
||
| 1174 | |||
| 1175 | 218 | $calc->addDependency($targetSubClass->name, $class->name, 1); |
|
| 1176 | } |
||
| 1177 | } |
||
| 1178 | } |
||
| 1179 | |||
| 1180 | 1015 | return $calc->sort(); |
|
| 1181 | } |
||
| 1182 | |||
| 1183 | /** |
||
| 1184 | * Schedules an entity for insertion into the database. |
||
| 1185 | * If the entity already has an identifier, it will be added to the identity map. |
||
| 1186 | * |
||
| 1187 | * @param object $entity The entity to schedule for insertion. |
||
| 1188 | * |
||
| 1189 | * @return void |
||
| 1190 | * |
||
| 1191 | * @throws ORMInvalidArgumentException |
||
| 1192 | * @throws \InvalidArgumentException |
||
| 1193 | */ |
||
| 1194 | 1040 | public function scheduleForInsert($entity) |
|
| 1195 | { |
||
| 1196 | 1040 | $oid = spl_object_hash($entity); |
|
| 1197 | |||
| 1198 | 1040 | if (isset($this->entityUpdates[$oid])) { |
|
| 1199 | throw new InvalidArgumentException("Dirty entity can not be scheduled for insertion."); |
||
| 1200 | } |
||
| 1201 | |||
| 1202 | 1040 | if (isset($this->entityDeletions[$oid])) { |
|
| 1203 | 1 | throw ORMInvalidArgumentException::scheduleInsertForRemovedEntity($entity); |
|
| 1204 | } |
||
| 1205 | 1040 | if (isset($this->originalEntityData[$oid]) && ! isset($this->entityInsertions[$oid])) { |
|
| 1206 | 1 | throw ORMInvalidArgumentException::scheduleInsertForManagedEntity($entity); |
|
| 1207 | } |
||
| 1208 | |||
| 1209 | 1040 | if (isset($this->entityInsertions[$oid])) { |
|
| 1210 | 1 | throw ORMInvalidArgumentException::scheduleInsertTwice($entity); |
|
| 1211 | } |
||
| 1212 | |||
| 1213 | 1040 | $this->entityInsertions[$oid] = $entity; |
|
| 1214 | |||
| 1215 | 1040 | if (isset($this->entityIdentifiers[$oid])) { |
|
| 1216 | 269 | $this->addToIdentityMap($entity); |
|
| 1217 | } |
||
| 1218 | |||
| 1219 | 1040 | if ($entity instanceof NotifyPropertyChanged) { |
|
| 1220 | 5 | $entity->addPropertyChangedListener($this); |
|
| 1221 | } |
||
| 1222 | 1040 | } |
|
| 1223 | |||
| 1224 | /** |
||
| 1225 | * Checks whether an entity is scheduled for insertion. |
||
| 1226 | * |
||
| 1227 | * @param object $entity |
||
| 1228 | * |
||
| 1229 | * @return boolean |
||
| 1230 | */ |
||
| 1231 | 633 | public function isScheduledForInsert($entity) |
|
| 1232 | { |
||
| 1233 | 633 | return isset($this->entityInsertions[spl_object_hash($entity)]); |
|
| 1234 | } |
||
| 1235 | |||
| 1236 | /** |
||
| 1237 | * Schedules an entity for being updated. |
||
| 1238 | * |
||
| 1239 | * @param object $entity The entity to schedule for being updated. |
||
| 1240 | * |
||
| 1241 | * @return void |
||
| 1242 | * |
||
| 1243 | * @throws ORMInvalidArgumentException |
||
| 1244 | */ |
||
| 1245 | 1 | public function scheduleForUpdate($entity) |
|
| 1246 | { |
||
| 1247 | 1 | $oid = spl_object_hash($entity); |
|
| 1248 | |||
| 1249 | 1 | if ( ! isset($this->entityIdentifiers[$oid])) { |
|
| 1250 | throw ORMInvalidArgumentException::entityHasNoIdentity($entity, "scheduling for update"); |
||
| 1251 | } |
||
| 1252 | |||
| 1253 | 1 | if (isset($this->entityDeletions[$oid])) { |
|
| 1254 | throw ORMInvalidArgumentException::entityIsRemoved($entity, "schedule for update"); |
||
| 1255 | } |
||
| 1256 | |||
| 1257 | 1 | if ( ! isset($this->entityUpdates[$oid]) && ! isset($this->entityInsertions[$oid])) { |
|
| 1258 | 1 | $this->entityUpdates[$oid] = $entity; |
|
| 1259 | } |
||
| 1260 | 1 | } |
|
| 1261 | |||
| 1262 | /** |
||
| 1263 | * INTERNAL: |
||
| 1264 | * Schedules an extra update that will be executed immediately after the |
||
| 1265 | * regular entity updates within the currently running commit cycle. |
||
| 1266 | * |
||
| 1267 | * Extra updates for entities are stored as (entity, changeset) tuples. |
||
| 1268 | * |
||
| 1269 | * @ignore |
||
| 1270 | * |
||
| 1271 | * @param object $entity The entity for which to schedule an extra update. |
||
| 1272 | * @param array $changeset The changeset of the entity (what to update). |
||
| 1273 | * |
||
| 1274 | * @return void |
||
| 1275 | */ |
||
| 1276 | 40 | public function scheduleExtraUpdate($entity, array $changeset) |
|
| 1277 | { |
||
| 1278 | 40 | $oid = spl_object_hash($entity); |
|
| 1279 | 40 | $extraUpdate = array($entity, $changeset); |
|
| 1280 | |||
| 1281 | 40 | if (isset($this->extraUpdates[$oid])) { |
|
| 1282 | 1 | list(, $changeset2) = $this->extraUpdates[$oid]; |
|
| 1283 | |||
| 1284 | 1 | $extraUpdate = array($entity, $changeset + $changeset2); |
|
| 1285 | } |
||
| 1286 | |||
| 1287 | 40 | $this->extraUpdates[$oid] = $extraUpdate; |
|
| 1288 | 40 | } |
|
| 1289 | |||
| 1290 | /** |
||
| 1291 | * Checks whether an entity is registered as dirty in the unit of work. |
||
| 1292 | * Note: Is not very useful currently as dirty entities are only registered |
||
| 1293 | * at commit time. |
||
| 1294 | * |
||
| 1295 | * @param object $entity |
||
| 1296 | * |
||
| 1297 | * @return boolean |
||
| 1298 | */ |
||
| 1299 | public function isScheduledForUpdate($entity) |
||
| 1300 | { |
||
| 1301 | return isset($this->entityUpdates[spl_object_hash($entity)]); |
||
| 1302 | } |
||
| 1303 | |||
| 1304 | /** |
||
| 1305 | * Checks whether an entity is registered to be checked in the unit of work. |
||
| 1306 | * |
||
| 1307 | * @param object $entity |
||
| 1308 | * |
||
| 1309 | * @return boolean |
||
| 1310 | */ |
||
| 1311 | 1 | public function isScheduledForDirtyCheck($entity) |
|
| 1312 | { |
||
| 1313 | 1 | $rootEntityName = $this->em->getClassMetadata(get_class($entity))->rootEntityName; |
|
| 1314 | |||
| 1315 | 1 | return isset($this->scheduledForSynchronization[$rootEntityName][spl_object_hash($entity)]); |
|
| 1316 | } |
||
| 1317 | |||
| 1318 | /** |
||
| 1319 | * INTERNAL: |
||
| 1320 | * Schedules an entity for deletion. |
||
| 1321 | * |
||
| 1322 | * @param object $entity |
||
| 1323 | * |
||
| 1324 | * @return void |
||
| 1325 | */ |
||
| 1326 | 66 | public function scheduleForDelete($entity) |
|
| 1327 | { |
||
| 1328 | 66 | $oid = spl_object_hash($entity); |
|
| 1329 | |||
| 1330 | 66 | if (isset($this->entityInsertions[$oid])) { |
|
| 1331 | 1 | if ($this->isInIdentityMap($entity)) { |
|
| 1332 | $this->removeFromIdentityMap($entity); |
||
| 1333 | } |
||
| 1334 | |||
| 1335 | 1 | unset($this->entityInsertions[$oid], $this->entityStates[$oid]); |
|
| 1336 | |||
| 1337 | 1 | return; // entity has not been persisted yet, so nothing more to do. |
|
| 1338 | } |
||
| 1339 | |||
| 1340 | 66 | if ( ! $this->isInIdentityMap($entity)) { |
|
| 1341 | 1 | return; |
|
| 1342 | } |
||
| 1343 | |||
| 1344 | 65 | $this->removeFromIdentityMap($entity); |
|
| 1345 | |||
| 1346 | 65 | unset($this->entityUpdates[$oid]); |
|
| 1347 | |||
| 1348 | 65 | if ( ! isset($this->entityDeletions[$oid])) { |
|
| 1349 | 65 | $this->entityDeletions[$oid] = $entity; |
|
| 1350 | 65 | $this->entityStates[$oid] = self::STATE_REMOVED; |
|
| 1351 | } |
||
| 1352 | 65 | } |
|
| 1353 | |||
| 1354 | /** |
||
| 1355 | * Checks whether an entity is registered as removed/deleted with the unit |
||
| 1356 | * of work. |
||
| 1357 | * |
||
| 1358 | * @param object $entity |
||
| 1359 | * |
||
| 1360 | * @return boolean |
||
| 1361 | */ |
||
| 1362 | 17 | public function isScheduledForDelete($entity) |
|
| 1363 | { |
||
| 1364 | 17 | return isset($this->entityDeletions[spl_object_hash($entity)]); |
|
| 1365 | } |
||
| 1366 | |||
| 1367 | /** |
||
| 1368 | * Checks whether an entity is scheduled for insertion, update or deletion. |
||
| 1369 | * |
||
| 1370 | * @param object $entity |
||
| 1371 | * |
||
| 1372 | * @return boolean |
||
| 1373 | */ |
||
| 1374 | public function isEntityScheduled($entity) |
||
| 1375 | { |
||
| 1376 | $oid = spl_object_hash($entity); |
||
| 1377 | |||
| 1378 | return isset($this->entityInsertions[$oid]) |
||
| 1379 | || isset($this->entityUpdates[$oid]) |
||
| 1380 | || isset($this->entityDeletions[$oid]); |
||
| 1381 | } |
||
| 1382 | |||
| 1383 | /** |
||
| 1384 | * INTERNAL: |
||
| 1385 | * Registers an entity in the identity map. |
||
| 1386 | * Note that entities in a hierarchy are registered with the class name of |
||
| 1387 | * the root entity. |
||
| 1388 | * |
||
| 1389 | * @ignore |
||
| 1390 | * |
||
| 1391 | * @param object $entity The entity to register. |
||
| 1392 | * |
||
| 1393 | * @return boolean TRUE if the registration was successful, FALSE if the identity of |
||
| 1394 | * the entity in question is already managed. |
||
| 1395 | * |
||
| 1396 | * @throws ORMInvalidArgumentException |
||
| 1397 | */ |
||
| 1398 | 1105 | public function addToIdentityMap($entity) |
|
| 1399 | { |
||
| 1400 | 1105 | $classMetadata = $this->em->getClassMetadata(get_class($entity)); |
|
| 1401 | 1105 | $identifier = $this->entityIdentifiers[spl_object_hash($entity)]; |
|
| 1402 | |||
| 1403 | 1105 | if (empty($identifier) || in_array(null, $identifier, true)) { |
|
| 1404 | 6 | throw ORMInvalidArgumentException::entityWithoutIdentity($classMetadata->name, $entity); |
|
| 1405 | } |
||
| 1406 | |||
| 1407 | 1099 | $idHash = implode(' ', $identifier); |
|
| 1408 | 1099 | $className = $classMetadata->rootEntityName; |
|
| 1409 | |||
| 1410 | 1099 | if (isset($this->identityMap[$className][$idHash])) { |
|
| 1411 | 83 | return false; |
|
| 1412 | } |
||
| 1413 | |||
| 1414 | 1099 | $this->identityMap[$className][$idHash] = $entity; |
|
| 1415 | |||
| 1416 | 1099 | return true; |
|
| 1417 | } |
||
| 1418 | |||
| 1419 | /** |
||
| 1420 | * Gets the state of an entity with regard to the current unit of work. |
||
| 1421 | * |
||
| 1422 | * @param object $entity |
||
| 1423 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 1424 | * This parameter can be set to improve performance of entity state detection |
||
| 1425 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 1426 | * is either known or does not matter for the caller of the method. |
||
| 1427 | * |
||
| 1428 | * @return int The entity state. |
||
| 1429 | */ |
||
| 1430 | 1053 | public function getEntityState($entity, $assume = null) |
|
| 1499 | |||
| 1500 | /** |
||
| 1501 | * INTERNAL: |
||
| 1502 | * Removes an entity from the identity map. This effectively detaches the |
||
| 1503 | * entity from the persistence management of Doctrine. |
||
| 1504 | * |
||
| 1505 | * @ignore |
||
| 1506 | * |
||
| 1507 | * @param object $entity |
||
| 1508 | * |
||
| 1509 | * @return boolean |
||
| 1510 | * |
||
| 1511 | * @throws ORMInvalidArgumentException |
||
| 1512 | */ |
||
| 1513 | 77 | public function removeFromIdentityMap($entity) |
|
| 1536 | |||
| 1537 | /** |
||
| 1538 | * INTERNAL: |
||
| 1539 | * Gets an entity in the identity map by its identifier hash. |
||
| 1540 | * |
||
| 1541 | * @ignore |
||
| 1542 | * |
||
| 1543 | * @param string $idHash |
||
| 1544 | * @param string $rootClassName |
||
| 1545 | * |
||
| 1546 | * @return object |
||
| 1547 | */ |
||
| 1548 | 6 | public function getByIdHash($idHash, $rootClassName) |
|
| 1552 | |||
| 1553 | /** |
||
| 1554 | * INTERNAL: |
||
| 1555 | * Tries to get an entity by its identifier hash. If no entity is found for |
||
| 1556 | * the given hash, FALSE is returned. |
||
| 1557 | * |
||
| 1558 | * @ignore |
||
| 1559 | * |
||
| 1560 | * @param mixed $idHash (must be possible to cast it to string) |
||
| 1561 | * @param string $rootClassName |
||
| 1562 | * |
||
| 1563 | * @return object|bool The found entity or FALSE. |
||
| 1564 | */ |
||
| 1565 | 34 | public function tryGetByIdHash($idHash, $rootClassName) |
|
| 1573 | |||
| 1574 | /** |
||
| 1575 | * Checks whether an entity is registered in the identity map of this UnitOfWork. |
||
| 1576 | * |
||
| 1577 | * @param object $entity |
||
| 1578 | * |
||
| 1579 | * @return boolean |
||
| 1580 | */ |
||
| 1581 | 214 | public function isInIdentityMap($entity) |
|
| 1594 | |||
| 1595 | /** |
||
| 1596 | * INTERNAL: |
||
| 1597 | * Checks whether an identifier hash exists in the identity map. |
||
| 1598 | * |
||
| 1599 | * @ignore |
||
| 1600 | * |
||
| 1601 | * @param string $idHash |
||
| 1602 | * @param string $rootClassName |
||
| 1603 | * |
||
| 1604 | * @return boolean |
||
| 1605 | */ |
||
| 1606 | public function containsIdHash($idHash, $rootClassName) |
||
| 1610 | |||
| 1611 | /** |
||
| 1612 | * Persists an entity as part of the current unit of work. |
||
| 1613 | * |
||
| 1614 | * @param object $entity The entity to persist. |
||
| 1615 | * |
||
| 1616 | * @return void |
||
| 1617 | */ |
||
| 1618 | 1036 | public function persist($entity) |
|
| 1624 | |||
| 1625 | /** |
||
| 1626 | * Persists an entity as part of the current unit of work. |
||
| 1627 | * |
||
| 1628 | * This method is internally called during persist() cascades as it tracks |
||
| 1629 | * the already visited entities to prevent infinite recursions. |
||
| 1630 | * |
||
| 1631 | * @param object $entity The entity to persist. |
||
| 1632 | * @param array $visited The already visited entities. |
||
| 1633 | * |
||
| 1634 | * @return void |
||
| 1635 | * |
||
| 1636 | * @throws ORMInvalidArgumentException |
||
| 1637 | * @throws UnexpectedValueException |
||
| 1638 | */ |
||
| 1639 | 1036 | private function doPersist($entity, array &$visited) |
|
| 1687 | |||
| 1688 | /** |
||
| 1689 | * Deletes an entity as part of the current unit of work. |
||
| 1690 | * |
||
| 1691 | * @param object $entity The entity to remove. |
||
| 1692 | * |
||
| 1693 | * @return void |
||
| 1694 | */ |
||
| 1695 | 65 | public function remove($entity) |
|
| 1701 | |||
| 1702 | /** |
||
| 1703 | * Deletes an entity as part of the current unit of work. |
||
| 1704 | * |
||
| 1705 | * This method is internally called during delete() cascades as it tracks |
||
| 1706 | * the already visited entities to prevent infinite recursions. |
||
| 1707 | * |
||
| 1708 | * @param object $entity The entity to delete. |
||
| 1709 | * @param array $visited The map of the already visited entities. |
||
| 1710 | * |
||
| 1711 | * @return void |
||
| 1712 | * |
||
| 1713 | * @throws ORMInvalidArgumentException If the instance is a detached entity. |
||
| 1714 | * @throws UnexpectedValueException |
||
| 1715 | */ |
||
| 1716 | 65 | private function doRemove($entity, array &$visited) |
|
| 1756 | |||
| 1757 | /** |
||
| 1758 | * Merges the state of the given detached entity into this UnitOfWork. |
||
| 1759 | * |
||
| 1760 | * @param object $entity |
||
| 1761 | * |
||
| 1762 | * @return object The managed copy of the entity. |
||
| 1763 | * |
||
| 1764 | * @throws OptimisticLockException If the entity uses optimistic locking through a version |
||
| 1765 | * attribute and the version check against the managed copy fails. |
||
| 1766 | * |
||
| 1767 | * @todo Require active transaction!? OptimisticLockException may result in undefined state!? |
||
| 1768 | */ |
||
| 1769 | 41 | public function merge($entity) |
|
| 1775 | |||
| 1776 | /** |
||
| 1777 | * Executes a merge operation on an entity. |
||
| 1778 | * |
||
| 1779 | * @param object $entity |
||
| 1780 | * @param array $visited |
||
| 1781 | * @param object|null $prevManagedCopy |
||
| 1782 | * @param array|null $assoc |
||
| 1783 | * |
||
| 1784 | * @return object The managed copy of the entity. |
||
| 1785 | * |
||
| 1786 | * @throws OptimisticLockException If the entity uses optimistic locking through a version |
||
| 1787 | * attribute and the version check against the managed copy fails. |
||
| 1788 | * @throws ORMInvalidArgumentException If the entity instance is NEW. |
||
| 1789 | * @throws EntityNotFoundException |
||
| 1790 | */ |
||
| 1791 | 41 | private function doMerge($entity, array &$visited, $prevManagedCopy = null, array $assoc = []) |
|
| 1893 | |||
| 1894 | /** |
||
| 1895 | * Tests if an entity is loaded - must either be a loaded proxy or not a proxy |
||
| 1896 | * |
||
| 1897 | * @param object $entity |
||
| 1898 | * |
||
| 1899 | * @return bool |
||
| 1900 | */ |
||
| 1901 | 39 | private function isLoaded($entity) |
|
| 1905 | |||
| 1906 | /** |
||
| 1907 | * Sets/adds associated managed copies into the previous entity's association field |
||
| 1908 | * |
||
| 1909 | * @param object $entity |
||
| 1910 | * @param array $association |
||
| 1911 | * @param object $previousManagedCopy |
||
| 1912 | * @param object $managedCopy |
||
| 1913 | * |
||
| 1914 | * @return void |
||
| 1915 | */ |
||
| 1916 | 6 | private function updateAssociationWithMergedEntity($entity, array $association, $previousManagedCopy, $managedCopy) |
|
| 1936 | |||
| 1937 | /** |
||
| 1938 | * Detaches an entity from the persistence management. It's persistence will |
||
| 1939 | * no longer be managed by Doctrine. |
||
| 1940 | * |
||
| 1941 | * @param object $entity The entity to detach. |
||
| 1942 | * |
||
| 1943 | * @return void |
||
| 1944 | */ |
||
| 1945 | 12 | public function detach($entity) |
|
| 1951 | |||
| 1952 | /** |
||
| 1953 | * Executes a detach operation on the given entity. |
||
| 1954 | * |
||
| 1955 | * @param object $entity |
||
| 1956 | * @param array $visited |
||
| 1957 | * @param boolean $noCascade if true, don't cascade detach operation. |
||
| 1958 | * |
||
| 1959 | * @return void |
||
| 1960 | */ |
||
| 1961 | 15 | private function doDetach($entity, array &$visited, $noCascade = false) |
|
| 1995 | |||
| 1996 | /** |
||
| 1997 | * Refreshes the state of the given entity from the database, overwriting |
||
| 1998 | * any local, unpersisted changes. |
||
| 1999 | * |
||
| 2000 | * @param object $entity The entity to refresh. |
||
| 2001 | * |
||
| 2002 | * @return void |
||
| 2003 | * |
||
| 2004 | * @throws InvalidArgumentException If the entity is not MANAGED. |
||
| 2005 | */ |
||
| 2006 | 17 | public function refresh($entity) |
|
| 2012 | |||
| 2013 | /** |
||
| 2014 | * Executes a refresh operation on an entity. |
||
| 2015 | * |
||
| 2016 | * @param object $entity The entity to refresh. |
||
| 2017 | * @param array $visited The already visited entities during cascades. |
||
| 2018 | * |
||
| 2019 | * @return void |
||
| 2020 | * |
||
| 2021 | * @throws ORMInvalidArgumentException If the entity is not MANAGED. |
||
| 2022 | */ |
||
| 2023 | 17 | private function doRefresh($entity, array &$visited) |
|
| 2046 | |||
| 2047 | /** |
||
| 2048 | * Cascades a refresh operation to associated entities. |
||
| 2049 | * |
||
| 2050 | * @param object $entity |
||
| 2051 | * @param array $visited |
||
| 2052 | * |
||
| 2053 | * @return void |
||
| 2054 | */ |
||
| 2055 | 17 | private function cascadeRefresh($entity, array &$visited) |
|
| 2089 | |||
| 2090 | /** |
||
| 2091 | * Cascades a detach operation to associated entities. |
||
| 2092 | * |
||
| 2093 | * @param object $entity |
||
| 2094 | * @param array $visited |
||
| 2095 | * |
||
| 2096 | * @return void |
||
| 2097 | */ |
||
| 2098 | 13 | private function cascadeDetach($entity, array &$visited) |
|
| 2132 | |||
| 2133 | /** |
||
| 2134 | * Cascades a merge operation to associated entities. |
||
| 2135 | * |
||
| 2136 | * @param object $entity |
||
| 2137 | * @param object $managedCopy |
||
| 2138 | * @param array $visited |
||
| 2139 | * |
||
| 2140 | * @return void |
||
| 2141 | */ |
||
| 2142 | 39 | private function cascadeMerge($entity, $managedCopy, array &$visited) |
|
| 2172 | |||
| 2173 | /** |
||
| 2174 | * Cascades the save operation to associated entities. |
||
| 2175 | * |
||
| 2176 | * @param object $entity |
||
| 2177 | * @param array $visited |
||
| 2178 | * |
||
| 2179 | * @return void |
||
| 2180 | */ |
||
| 2181 | 1036 | private function cascadePersist($entity, array &$visited) |
|
| 2232 | |||
| 2233 | /** |
||
| 2234 | * Cascades the delete operation to associated entities. |
||
| 2235 | * |
||
| 2236 | * @param object $entity |
||
| 2237 | * @param array $visited |
||
| 2238 | * |
||
| 2239 | * @return void |
||
| 2240 | */ |
||
| 2241 | 65 | private function cascadeRemove($entity, array &$visited) |
|
| 2281 | |||
| 2282 | /** |
||
| 2283 | * Acquire a lock on the given entity. |
||
| 2284 | * |
||
| 2285 | * @param object $entity |
||
| 2286 | * @param int $lockMode |
||
| 2287 | * @param int $lockVersion |
||
| 2288 | * |
||
| 2289 | * @return void |
||
| 2290 | * |
||
| 2291 | * @throws ORMInvalidArgumentException |
||
| 2292 | * @throws TransactionRequiredException |
||
| 2293 | * @throws OptimisticLockException |
||
| 2294 | */ |
||
| 2295 | 11 | public function lock($entity, $lockMode, $lockVersion = null) |
|
| 2348 | |||
| 2349 | /** |
||
| 2350 | * Gets the CommitOrderCalculator used by the UnitOfWork to order commits. |
||
| 2351 | * |
||
| 2352 | * @return \Doctrine\ORM\Internal\CommitOrderCalculator |
||
| 2353 | */ |
||
| 2354 | 1015 | public function getCommitOrderCalculator() |
|
| 2358 | |||
| 2359 | /** |
||
| 2360 | * Clears the UnitOfWork. |
||
| 2361 | * |
||
| 2362 | * @param string|null $entityName if given, only entities of this type will get detached. |
||
| 2363 | * |
||
| 2364 | * @return void |
||
| 2365 | */ |
||
| 2366 | 1227 | public function clear($entityName = null) |
|
| 2393 | |||
| 2394 | /** |
||
| 2395 | * INTERNAL: |
||
| 2396 | * Schedules an orphaned entity for removal. The remove() operation will be |
||
| 2397 | * invoked on that entity at the beginning of the next commit of this |
||
| 2398 | * UnitOfWork. |
||
| 2399 | * |
||
| 2400 | * @ignore |
||
| 2401 | * |
||
| 2402 | * @param object $entity |
||
| 2403 | * |
||
| 2404 | * @return void |
||
| 2405 | */ |
||
| 2406 | 17 | public function scheduleOrphanRemoval($entity) |
|
| 2410 | |||
| 2411 | /** |
||
| 2412 | * INTERNAL: |
||
| 2413 | * Cancels a previously scheduled orphan removal. |
||
| 2414 | * |
||
| 2415 | * @ignore |
||
| 2416 | * |
||
| 2417 | * @param object $entity |
||
| 2418 | * |
||
| 2419 | * @return void |
||
| 2420 | */ |
||
| 2421 | 114 | public function cancelOrphanRemoval($entity) |
|
| 2425 | |||
| 2426 | /** |
||
| 2427 | * INTERNAL: |
||
| 2428 | * Schedules a complete collection for removal when this UnitOfWork commits. |
||
| 2429 | * |
||
| 2430 | * @param PersistentCollection $coll |
||
| 2431 | * |
||
| 2432 | * @return void |
||
| 2433 | */ |
||
| 2434 | 13 | public function scheduleCollectionDeletion(PersistentCollection $coll) |
|
| 2444 | |||
| 2445 | /** |
||
| 2446 | * @param PersistentCollection $coll |
||
| 2447 | * |
||
| 2448 | * @return bool |
||
| 2449 | */ |
||
| 2450 | public function isCollectionScheduledForDeletion(PersistentCollection $coll) |
||
| 2454 | |||
| 2455 | /** |
||
| 2456 | * @param ClassMetadata $class |
||
| 2457 | * |
||
| 2458 | * @return \Doctrine\Common\Persistence\ObjectManagerAware|object |
||
| 2459 | */ |
||
| 2460 | 677 | private function newInstance($class) |
|
| 2470 | |||
| 2471 | /** |
||
| 2472 | * INTERNAL: |
||
| 2473 | * Creates an entity. Used for reconstitution of persistent entities. |
||
| 2474 | * |
||
| 2475 | * Internal note: Highly performance-sensitive method. |
||
| 2476 | * |
||
| 2477 | * @ignore |
||
| 2478 | * |
||
| 2479 | * @param string $className The name of the entity class. |
||
| 2480 | * @param array $data The data for the entity. |
||
| 2481 | * @param array $hints Any hints to account for during reconstitution/lookup of the entity. |
||
| 2482 | * |
||
| 2483 | * @return object The managed entity instance. |
||
| 2484 | * |
||
| 2485 | * @todo Rename: getOrCreateEntity |
||
| 2486 | */ |
||
| 2487 | 815 | public function createEntity($className, array $data, &$hints = array()) |
|
| 2771 | |||
| 2772 | /** |
||
| 2773 | * @return void |
||
| 2774 | */ |
||
| 2775 | 871 | public function triggerEagerLoads() |
|
| 2797 | |||
| 2798 | /** |
||
| 2799 | * Initializes (loads) an uninitialized persistent collection of an entity. |
||
| 2800 | * |
||
| 2801 | * @param \Doctrine\ORM\PersistentCollection $collection The collection to initialize. |
||
| 2802 | * |
||
| 2803 | * @return void |
||
| 2804 | * |
||
| 2805 | * @todo Maybe later move to EntityManager#initialize($proxyOrCollection). See DDC-733. |
||
| 2806 | */ |
||
| 2807 | 144 | public function loadCollection(PersistentCollection $collection) |
|
| 2824 | |||
| 2825 | /** |
||
| 2826 | * Gets the identity map of the UnitOfWork. |
||
| 2827 | * |
||
| 2828 | * @return array |
||
| 2829 | */ |
||
| 2830 | 2 | public function getIdentityMap() |
|
| 2834 | |||
| 2835 | /** |
||
| 2836 | * Gets the original data of an entity. The original data is the data that was |
||
| 2837 | * present at the time the entity was reconstituted from the database. |
||
| 2838 | * |
||
| 2839 | * @param object $entity |
||
| 2840 | * |
||
| 2841 | * @return array |
||
| 2842 | */ |
||
| 2843 | 119 | public function getOriginalEntityData($entity) |
|
| 2851 | |||
| 2852 | /** |
||
| 2853 | * @ignore |
||
| 2854 | * |
||
| 2855 | * @param object $entity |
||
| 2856 | * @param array $data |
||
| 2857 | * |
||
| 2858 | * @return void |
||
| 2859 | */ |
||
| 2860 | public function setOriginalEntityData($entity, array $data) |
||
| 2864 | |||
| 2865 | /** |
||
| 2866 | * INTERNAL: |
||
| 2867 | * Sets a property value of the original data array of an entity. |
||
| 2868 | * |
||
| 2869 | * @ignore |
||
| 2870 | * |
||
| 2871 | * @param string $oid |
||
| 2872 | * @param string $property |
||
| 2873 | * @param mixed $value |
||
| 2874 | * |
||
| 2875 | * @return void |
||
| 2876 | */ |
||
| 2877 | 314 | public function setOriginalEntityProperty($oid, $property, $value) |
|
| 2881 | |||
| 2882 | /** |
||
| 2883 | * Gets the identifier of an entity. |
||
| 2884 | * The returned value is always an array of identifier values. If the entity |
||
| 2885 | * has a composite identifier then the identifier values are in the same |
||
| 2886 | * order as the identifier field names as returned by ClassMetadata#getIdentifierFieldNames(). |
||
| 2887 | * |
||
| 2888 | * @param object $entity |
||
| 2889 | * |
||
| 2890 | * @return array The identifier values. |
||
| 2891 | */ |
||
| 2892 | 847 | public function getEntityIdentifier($entity) |
|
| 2896 | |||
| 2897 | /** |
||
| 2898 | * Processes an entity instance to extract their identifier values. |
||
| 2899 | * |
||
| 2900 | * @param object $entity The entity instance. |
||
| 2901 | * |
||
| 2902 | * @return mixed A scalar value. |
||
| 2903 | * |
||
| 2904 | * @throws \Doctrine\ORM\ORMInvalidArgumentException |
||
| 2905 | */ |
||
| 2906 | 127 | public function getSingleIdentifierValue($entity) |
|
| 2920 | |||
| 2921 | /** |
||
| 2922 | * Tries to find an entity with the given identifier in the identity map of |
||
| 2923 | * this UnitOfWork. |
||
| 2924 | * |
||
| 2925 | * @param mixed $id The entity identifier to look for. |
||
| 2926 | * @param string $rootClassName The name of the root class of the mapped entity hierarchy. |
||
| 2927 | * |
||
| 2928 | * @return object|bool Returns the entity with the specified identifier if it exists in |
||
| 2929 | * this UnitOfWork, FALSE otherwise. |
||
| 2930 | */ |
||
| 2931 | 525 | public function tryGetById($id, $rootClassName) |
|
| 2939 | |||
| 2940 | /** |
||
| 2941 | * Schedules an entity for dirty-checking at commit-time. |
||
| 2942 | * |
||
| 2943 | * @param object $entity The entity to schedule for dirty-checking. |
||
| 2944 | * |
||
| 2945 | * @return void |
||
| 2946 | * |
||
| 2947 | * @todo Rename: scheduleForSynchronization |
||
| 2948 | */ |
||
| 2949 | 5 | public function scheduleForDirtyCheck($entity) |
|
| 2955 | |||
| 2956 | /** |
||
| 2957 | * Checks whether the UnitOfWork has any pending insertions. |
||
| 2958 | * |
||
| 2959 | * @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
||
| 2960 | */ |
||
| 2961 | public function hasPendingInsertions() |
||
| 2965 | |||
| 2966 | /** |
||
| 2967 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
||
| 2968 | * number of entities in the identity map. |
||
| 2969 | * |
||
| 2970 | * @return integer |
||
| 2971 | */ |
||
| 2972 | 1 | public function size() |
|
| 2978 | |||
| 2979 | /** |
||
| 2980 | * Gets the EntityPersister for an Entity. |
||
| 2981 | * |
||
| 2982 | * @param string $entityName The name of the Entity. |
||
| 2983 | * |
||
| 2984 | * @return \Doctrine\ORM\Persisters\Entity\EntityPersister |
||
| 2985 | */ |
||
| 2986 | 1077 | public function getEntityPersister($entityName) |
|
| 3022 | |||
| 3023 | /** |
||
| 3024 | * Gets a collection persister for a collection-valued association. |
||
| 3025 | * |
||
| 3026 | * @param array $association |
||
| 3027 | * |
||
| 3028 | * @return \Doctrine\ORM\Persisters\Collection\CollectionPersister |
||
| 3029 | */ |
||
| 3030 | 572 | public function getCollectionPersister(array $association) |
|
| 3055 | |||
| 3056 | /** |
||
| 3057 | * INTERNAL: |
||
| 3058 | * Registers an entity as managed. |
||
| 3059 | * |
||
| 3060 | * @param object $entity The entity. |
||
| 3061 | * @param array $id The identifier values. |
||
| 3062 | * @param array $data The original entity data. |
||
| 3063 | * |
||
| 3064 | * @return void |
||
| 3065 | */ |
||
| 3066 | 206 | public function registerManaged($entity, array $id, array $data) |
|
| 3080 | |||
| 3081 | /** |
||
| 3082 | * INTERNAL: |
||
| 3083 | * Clears the property changeset of the entity with the given OID. |
||
| 3084 | * |
||
| 3085 | * @param string $oid The entity's OID. |
||
| 3086 | * |
||
| 3087 | * @return void |
||
| 3088 | */ |
||
| 3089 | public function clearEntityChangeSet($oid) |
||
| 3093 | |||
| 3094 | /* PropertyChangedListener implementation */ |
||
| 3095 | |||
| 3096 | /** |
||
| 3097 | * Notifies this UnitOfWork of a property change in an entity. |
||
| 3098 | * |
||
| 3099 | * @param object $entity The entity that owns the property. |
||
| 3100 | * @param string $propertyName The name of the property that changed. |
||
| 3101 | * @param mixed $oldValue The old value of the property. |
||
| 3102 | * @param mixed $newValue The new value of the property. |
||
| 3103 | * |
||
| 3104 | * @return void |
||
| 3105 | */ |
||
| 3106 | 3 | public function propertyChanged($entity, $propertyName, $oldValue, $newValue) |
|
| 3124 | |||
| 3125 | /** |
||
| 3126 | * Gets the currently scheduled entity insertions in this UnitOfWork. |
||
| 3127 | * |
||
| 3128 | * @return array |
||
| 3129 | */ |
||
| 3130 | 2 | public function getScheduledEntityInsertions() |
|
| 3134 | |||
| 3135 | /** |
||
| 3136 | * Gets the currently scheduled entity updates in this UnitOfWork. |
||
| 3137 | * |
||
| 3138 | * @return array |
||
| 3139 | */ |
||
| 3140 | 2 | public function getScheduledEntityUpdates() |
|
| 3144 | |||
| 3145 | /** |
||
| 3146 | * Gets the currently scheduled entity deletions in this UnitOfWork. |
||
| 3147 | * |
||
| 3148 | * @return array |
||
| 3149 | */ |
||
| 3150 | 1 | public function getScheduledEntityDeletions() |
|
| 3154 | |||
| 3155 | /** |
||
| 3156 | * Gets the currently scheduled complete collection deletions |
||
| 3157 | * |
||
| 3158 | * @return array |
||
| 3159 | */ |
||
| 3160 | 1 | public function getScheduledCollectionDeletions() |
|
| 3164 | |||
| 3165 | /** |
||
| 3166 | * Gets the currently scheduled collection inserts, updates and deletes. |
||
| 3167 | * |
||
| 3168 | * @return array |
||
| 3169 | */ |
||
| 3170 | public function getScheduledCollectionUpdates() |
||
| 3174 | |||
| 3175 | /** |
||
| 3176 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 3177 | * |
||
| 3178 | * @param object $obj |
||
| 3179 | * |
||
| 3180 | * @return void |
||
| 3181 | */ |
||
| 3182 | 2 | public function initializeObject($obj) |
|
| 3194 | |||
| 3195 | /** |
||
| 3196 | * Helper method to show an object as string. |
||
| 3197 | * |
||
| 3198 | * @param object $obj |
||
| 3199 | * |
||
| 3200 | * @return string |
||
| 3201 | */ |
||
| 3202 | 1 | private static function objToStr($obj) |
|
| 3206 | |||
| 3207 | /** |
||
| 3208 | * Marks an entity as read-only so that it will not be considered for updates during UnitOfWork#commit(). |
||
| 3209 | * |
||
| 3210 | * This operation cannot be undone as some parts of the UnitOfWork now keep gathering information |
||
| 3211 | * on this object that might be necessary to perform a correct update. |
||
| 3212 | * |
||
| 3213 | * @param object $object |
||
| 3214 | * |
||
| 3215 | * @return void |
||
| 3216 | * |
||
| 3217 | * @throws ORMInvalidArgumentException |
||
| 3218 | */ |
||
| 3219 | 6 | public function markReadOnly($object) |
|
| 3227 | |||
| 3228 | /** |
||
| 3229 | * Is this entity read only? |
||
| 3230 | * |
||
| 3231 | * @param object $object |
||
| 3232 | * |
||
| 3233 | * @return bool |
||
| 3234 | * |
||
| 3235 | * @throws ORMInvalidArgumentException |
||
| 3236 | */ |
||
| 3237 | 3 | public function isReadOnly($object) |
|
| 3245 | |||
| 3246 | /** |
||
| 3247 | * Perform whatever processing is encapsulated here after completion of the transaction. |
||
| 3248 | */ |
||
| 3249 | 1010 | private function afterTransactionComplete() |
|
| 3255 | |||
| 3256 | /** |
||
| 3257 | * Perform whatever processing is encapsulated here after completion of the rolled-back. |
||
| 3258 | */ |
||
| 3259 | private function afterTransactionRolledBack() |
||
| 3265 | |||
| 3266 | /** |
||
| 3267 | * Performs an action after the transaction. |
||
| 3268 | * |
||
| 3269 | * @param callable $callback |
||
| 3270 | */ |
||
| 3271 | 1015 | private function performCallbackOnCachedPersister(callable $callback) |
|
| 3283 | |||
| 3284 | 1019 | private function dispatchOnFlushEvent() |
|
| 3290 | |||
| 3291 | 1014 | private function dispatchPostFlushEvent() |
|
| 3297 | |||
| 3298 | /** |
||
| 3299 | * Verifies if two given entities actually are the same based on identifier comparison |
||
| 3300 | * |
||
| 3301 | * @param object $entity1 |
||
| 3302 | * @param object $entity2 |
||
| 3303 | * |
||
| 3304 | * @return bool |
||
| 3305 | */ |
||
| 3306 | 14 | private function isIdentifierEquals($entity1, $entity2) |
|
| 3330 | |||
| 3331 | /** |
||
| 3332 | * @param object $entity |
||
| 3333 | * @param object $managedCopy |
||
| 3334 | * |
||
| 3335 | * @throws ORMException |
||
| 3336 | * @throws OptimisticLockException |
||
| 3337 | * @throws TransactionRequiredException |
||
| 3338 | */ |
||
| 3339 | 31 | private function mergeEntityStateIntoManagedCopy($entity, $managedCopy) |
|
| 3430 | |||
| 3431 | /** |
||
| 3432 | * This method called by hydrators, and indicates that hydrator totally completed current hydration cycle. |
||
| 3433 | * Unit of work able to fire deferred events, related to loading events here. |
||
| 3434 | * |
||
| 3435 | * @internal should be called internally from object hydrators |
||
| 3436 | */ |
||
| 3437 | 883 | public function hydrationComplete() |
|
| 3441 | |||
| 3442 | /** |
||
| 3443 | * @param string $entityName |
||
| 3444 | */ |
||
| 3445 | 3 | private function clearIdentityMapForEntityName($entityName) |
|
| 3457 | |||
| 3458 | /** |
||
| 3459 | * @param string $entityName |
||
| 3460 | */ |
||
| 3461 | 3 | private function clearEntityInsertionsForEntityName($entityName) |
|
| 3470 | } |
||
| 3471 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.