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 | 2327 | public function __construct(EntityManagerInterface $em) |
|
| 293 | { |
||
| 294 | 2327 | $this->em = $em; |
|
| 295 | 2327 | $this->evm = $em->getEventManager(); |
|
| 296 | 2327 | $this->listenersInvoker = new ListenersInvoker($em); |
|
| 297 | 2327 | $this->hasCache = $em->getConfiguration()->isSecondLevelCacheEnabled(); |
|
| 298 | 2327 | $this->identifierFlattener = new IdentifierFlattener($this, $em->getMetadataFactory()); |
|
| 299 | 2327 | $this->hydrationCompleteHandler = new HydrationCompleteHandler($this->listenersInvoker, $em); |
|
| 300 | 2327 | $this->reflectionPropertiesGetter = new ReflectionPropertiesGetter(new RuntimeReflectionService()); |
|
| 301 | 2327 | } |
|
| 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 | 1014 | public function commit($entity = null) |
|
| 323 | { |
||
| 324 | // Raise preFlush |
||
| 325 | 1014 | 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 | 1014 | if ($entity === null) { |
|
| 331 | 1006 | $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 | 1011 | if ( ! ($this->entityInsertions || |
|
|
|
|||
| 341 | 166 | $this->entityDeletions || |
|
| 342 | 130 | $this->entityUpdates || |
|
| 343 | 40 | $this->collectionUpdates || |
|
| 344 | 37 | $this->collectionDeletions || |
|
| 345 | 1011 | $this->orphanRemovals)) { |
|
| 346 | 25 | $this->dispatchOnFlushEvent(); |
|
| 347 | 25 | $this->dispatchPostFlushEvent(); |
|
| 348 | |||
| 349 | 25 | return; // Nothing to do. |
|
| 350 | } |
||
| 351 | |||
| 352 | 1007 | if ($this->orphanRemovals) { |
|
| 353 | 16 | foreach ($this->orphanRemovals as $orphan) { |
|
| 354 | 16 | $this->remove($orphan); |
|
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | 1007 | $this->dispatchOnFlushEvent(); |
|
| 359 | |||
| 360 | // Now we need a commit order to maintain referential integrity |
||
| 361 | 1007 | $commitOrder = $this->getCommitOrder(); |
|
| 362 | |||
| 363 | 1007 | $conn = $this->em->getConnection(); |
|
| 364 | 1007 | $conn->beginTransaction(); |
|
| 365 | |||
| 366 | try { |
||
| 367 | // Collection deletions (deletions of complete collections) |
||
| 368 | 1007 | foreach ($this->collectionDeletions as $collectionToDelete) { |
|
| 369 | 19 | $this->getCollectionPersister($collectionToDelete->getMapping())->delete($collectionToDelete); |
|
| 370 | } |
||
| 371 | |||
| 372 | 1007 | if ($this->entityInsertions) { |
|
| 373 | 1003 | foreach ($commitOrder as $class) { |
|
| 374 | 1003 | $this->executeInserts($class); |
|
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | 1006 | 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 | 1002 | if ($this->extraUpdates) { |
|
| 386 | 40 | $this->executeExtraUpdates(); |
|
| 387 | } |
||
| 388 | |||
| 389 | // Collection updates (deleteRows, updateRows, insertRows) |
||
| 390 | 1002 | foreach ($this->collectionUpdates as $collectionToUpdate) { |
|
| 391 | 530 | $this->getCollectionPersister($collectionToUpdate->getMapping())->update($collectionToUpdate); |
|
| 392 | } |
||
| 393 | |||
| 394 | // Entity deletions come last and need to be in reverse commit order |
||
| 395 | 1002 | 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 | 1002 | $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 | 1002 | $this->afterTransactionComplete(); |
|
| 412 | |||
| 413 | // Take new snapshots from visited collections |
||
| 414 | 1002 | foreach ($this->visitedCollections as $coll) { |
|
| 415 | 529 | $coll->takeSnapshot(); |
|
| 416 | } |
||
| 417 | |||
| 418 | 1002 | $this->dispatchPostFlushEvent(); |
|
| 419 | |||
| 420 | // Clear up |
||
| 421 | 1001 | $this->entityInsertions = |
|
| 422 | 1001 | $this->entityUpdates = |
|
| 423 | 1001 | $this->entityDeletions = |
|
| 424 | 1001 | $this->extraUpdates = |
|
| 425 | 1001 | $this->entityChangeSets = |
|
| 426 | 1001 | $this->collectionUpdates = |
|
| 427 | 1001 | $this->collectionDeletions = |
|
| 428 | 1001 | $this->visitedCollections = |
|
| 429 | 1001 | $this->scheduledForSynchronization = |
|
| 430 | 1001 | $this->orphanRemovals = array(); |
|
| 431 | 1001 | } |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Computes the changesets of all entities scheduled for insertion. |
||
| 435 | * |
||
| 436 | * @return void |
||
| 437 | */ |
||
| 438 | 1013 | private function computeScheduleInsertsChangeSets() |
|
| 439 | { |
||
| 440 | 1013 | foreach ($this->entityInsertions as $entity) { |
|
| 441 | 1005 | $class = $this->em->getClassMetadata(get_class($entity)); |
|
| 442 | |||
| 443 | 1005 | $this->computeChangeSet($class, $entity); |
|
| 444 | } |
||
| 445 | 1011 | } |
|
| 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) |
|
| 462 | { |
||
| 463 | 16 | $state = $this->getEntityState($entity); |
|
| 464 | |||
| 465 | 16 | if ($state !== self::STATE_MANAGED && $state !== self::STATE_REMOVED) { |
|
| 466 | 1 | throw new \InvalidArgumentException("Entity has to be managed or scheduled for removal for single computation " . self::objToStr($entity)); |
|
| 467 | } |
||
| 468 | |||
| 469 | 15 | $class = $this->em->getClassMetadata(get_class($entity)); |
|
| 470 | |||
| 471 | 15 | if ($state === self::STATE_MANAGED && $class->isChangeTrackingDeferredImplicit()) { |
|
| 472 | 14 | $this->persist($entity); |
|
| 473 | } |
||
| 474 | |||
| 475 | // Compute changes for INSERTed entities first. This must always happen even in this case. |
||
| 476 | 15 | $this->computeScheduleInsertsChangeSets(); |
|
| 477 | |||
| 478 | 15 | if ($class->isReadOnly) { |
|
| 479 | return; |
||
| 480 | } |
||
| 481 | |||
| 482 | // Ignore uninitialized proxy objects |
||
| 483 | 15 | if ($entity instanceof Proxy && ! $entity->__isInitialized__) { |
|
| 484 | 2 | return; |
|
| 485 | } |
||
| 486 | |||
| 487 | // Only MANAGED entities that are NOT SCHEDULED FOR INSERTION OR DELETION are processed here. |
||
| 488 | 13 | $oid = spl_object_hash($entity); |
|
| 489 | |||
| 490 | 13 | if ( ! isset($this->entityInsertions[$oid]) && ! isset($this->entityDeletions[$oid]) && isset($this->entityStates[$oid])) { |
|
| 491 | 6 | $this->computeChangeSet($class, $entity); |
|
| 492 | } |
||
| 493 | 12 | } |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Executes any extra updates that have been scheduled. |
||
| 497 | */ |
||
| 498 | 40 | private function executeExtraUpdates() |
|
| 499 | { |
||
| 500 | 40 | foreach ($this->extraUpdates as $oid => $update) { |
|
| 501 | 40 | list ($entity, $changeset) = $update; |
|
| 502 | |||
| 503 | 40 | $this->entityChangeSets[$oid] = $changeset; |
|
| 504 | 40 | $this->getEntityPersister(get_class($entity))->update($entity); |
|
| 505 | } |
||
| 506 | |||
| 507 | 40 | $this->extraUpdates = array(); |
|
| 508 | 40 | } |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Gets the changeset for an entity. |
||
| 512 | * |
||
| 513 | * @param object $entity |
||
| 514 | * |
||
| 515 | * @return array |
||
| 516 | */ |
||
| 517 | 1005 | public function & getEntityChangeSet($entity) |
|
| 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 | 1015 | public function computeChangeSet(ClassMetadata $class, $entity) |
|
| 564 | { |
||
| 565 | 1015 | $oid = spl_object_hash($entity); |
|
| 566 | |||
| 567 | 1015 | if (isset($this->readOnlyObjects[$oid])) { |
|
| 568 | 2 | return; |
|
| 569 | } |
||
| 570 | |||
| 571 | 1015 | if ( ! $class->isInheritanceTypeNone()) { |
|
| 572 | 307 | $class = $this->em->getClassMetadata(get_class($entity)); |
|
| 573 | } |
||
| 574 | |||
| 575 | 1015 | $invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::preFlush) & ~ListenersInvoker::INVOKE_MANAGER; |
|
| 576 | |||
| 577 | 1015 | if ($invoke !== ListenersInvoker::INVOKE_NONE) { |
|
| 578 | 137 | $this->listenersInvoker->invoke($class, Events::preFlush, $entity, new PreFlushEventArgs($this->em), $invoke); |
|
| 579 | } |
||
| 580 | |||
| 581 | 1015 | $actualData = array(); |
|
| 582 | |||
| 583 | 1015 | foreach ($class->reflFields as $name => $refProp) { |
|
| 584 | 1015 | $value = $refProp->getValue($entity); |
|
| 585 | |||
| 586 | 1015 | if ($class->isCollectionValuedAssociation($name) && $value !== null) { |
|
| 587 | 776 | if ($value instanceof PersistentCollection) { |
|
| 588 | 199 | if ($value->getOwner() === $entity) { |
|
| 589 | 199 | continue; |
|
| 590 | } |
||
| 591 | |||
| 592 | 5 | $value = new ArrayCollection($value->getValues()); |
|
| 593 | } |
||
| 594 | |||
| 595 | // If $value is not a Collection then use an ArrayCollection. |
||
| 596 | 771 | if ( ! $value instanceof Collection) { |
|
| 597 | 242 | $value = new ArrayCollection($value); |
|
| 598 | } |
||
| 599 | |||
| 600 | 771 | $assoc = $class->associationMappings[$name]; |
|
| 601 | |||
| 602 | // Inject PersistentCollection |
||
| 603 | 771 | $value = new PersistentCollection( |
|
| 604 | 771 | $this->em, $this->em->getClassMetadata($assoc['targetEntity']), $value |
|
| 605 | ); |
||
| 606 | 771 | $value->setOwner($entity, $assoc); |
|
| 607 | 771 | $value->setDirty( ! $value->isEmpty()); |
|
| 608 | |||
| 609 | 771 | $class->reflFields[$name]->setValue($entity, $value); |
|
| 610 | |||
| 611 | 771 | $actualData[$name] = $value; |
|
| 612 | |||
| 613 | 771 | continue; |
|
| 614 | } |
||
| 615 | |||
| 616 | 1015 | if (( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) && ($name !== $class->versionField)) { |
|
| 617 | 1015 | $actualData[$name] = $value; |
|
| 618 | } |
||
| 619 | } |
||
| 620 | |||
| 621 | 1015 | 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 | 1011 | $this->originalEntityData[$oid] = $actualData; |
|
| 625 | 1011 | $changeSet = array(); |
|
| 626 | |||
| 627 | 1011 | foreach ($actualData as $propName => $actualValue) { |
|
| 628 | 996 | if ( ! isset($class->associationMappings[$propName])) { |
|
| 629 | 945 | $changeSet[$propName] = array(null, $actualValue); |
|
| 630 | |||
| 631 | 945 | continue; |
|
| 632 | } |
||
| 633 | |||
| 634 | 893 | $assoc = $class->associationMappings[$propName]; |
|
| 635 | |||
| 636 | 893 | if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) { |
|
| 637 | 893 | $changeSet[$propName] = array(null, $actualValue); |
|
| 638 | } |
||
| 639 | } |
||
| 640 | |||
| 641 | 1011 | $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 | 263 | $originalData = $this->originalEntityData[$oid]; |
|
| 646 | 263 | $isChangeTrackingNotify = $class->isChangeTrackingNotify(); |
|
| 647 | 263 | $changeSet = ($isChangeTrackingNotify && isset($this->entityChangeSets[$oid])) |
|
| 648 | ? $this->entityChangeSets[$oid] |
||
| 649 | 263 | : array(); |
|
| 650 | |||
| 651 | 263 | foreach ($actualData as $propName => $actualValue) { |
|
| 652 | // skip field, its a partially omitted one! |
||
| 653 | 248 | if ( ! (isset($originalData[$propName]) || array_key_exists($propName, $originalData))) { |
|
| 654 | 8 | continue; |
|
| 655 | } |
||
| 656 | |||
| 657 | 248 | $orgValue = $originalData[$propName]; |
|
| 658 | |||
| 659 | // skip if value haven't changed |
||
| 660 | 248 | if ($orgValue === $actualValue) { |
|
| 661 | 232 | 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 | 263 | 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 | 1015 | foreach ($class->associationMappings as $field => $assoc) { |
|
| 728 | 893 | if (($val = $class->reflFields[$field]->getValue($entity)) === null) { |
|
| 729 | 638 | continue; |
|
| 730 | } |
||
| 731 | |||
| 732 | 864 | $this->computeAssociationChanges($assoc, $val); |
|
| 733 | |||
| 734 | 856 | if ( ! isset($this->entityChangeSets[$oid]) && |
|
| 735 | 856 | $assoc['isOwningSide'] && |
|
| 736 | 856 | $assoc['type'] == ClassMetadata::MANY_TO_MANY && |
|
| 737 | 856 | $val instanceof PersistentCollection && |
|
| 738 | 856 | $val->isDirty()) { |
|
| 739 | |||
| 740 | 35 | $this->entityChangeSets[$oid] = array(); |
|
| 741 | 35 | $this->originalEntityData[$oid] = $actualData; |
|
| 742 | 856 | $this->entityUpdates[$oid] = $entity; |
|
| 743 | } |
||
| 744 | } |
||
| 745 | 1007 | } |
|
| 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 | 1006 | public function computeChangeSets() |
|
| 755 | { |
||
| 756 | // Compute changes for INSERTed entities first. This must always happen. |
||
| 757 | 1006 | $this->computeScheduleInsertsChangeSets(); |
|
| 758 | |||
| 759 | // Compute changes for other MANAGED entities. Change tracking policies take effect here. |
||
| 760 | 1004 | foreach ($this->identityMap as $className => $entities) { |
|
| 761 | 447 | $class = $this->em->getClassMetadata($className); |
|
| 762 | |||
| 763 | // Skip class if instances are read-only |
||
| 764 | 447 | if ($class->isReadOnly) { |
|
| 765 | 1 | continue; |
|
| 766 | } |
||
| 767 | |||
| 768 | // If change tracking is explicit or happens through notification, then only compute |
||
| 769 | // changes on entities of that type that are explicitly marked for synchronization. |
||
| 770 | switch (true) { |
||
| 771 | 446 | case ($class->isChangeTrackingDeferredImplicit()): |
|
| 772 | 444 | $entitiesToProcess = $entities; |
|
| 773 | 444 | break; |
|
| 774 | |||
| 775 | 3 | case (isset($this->scheduledForSynchronization[$className])): |
|
| 776 | 3 | $entitiesToProcess = $this->scheduledForSynchronization[$className]; |
|
| 777 | 3 | break; |
|
| 778 | |||
| 779 | default: |
||
| 780 | 1 | $entitiesToProcess = array(); |
|
| 781 | |||
| 782 | } |
||
| 783 | |||
| 784 | 446 | foreach ($entitiesToProcess as $entity) { |
|
| 785 | // Ignore uninitialized proxy objects |
||
| 786 | 426 | if ($entity instanceof Proxy && ! $entity->__isInitialized__) { |
|
| 787 | 34 | continue; |
|
| 788 | } |
||
| 789 | |||
| 790 | // Only MANAGED entities that are NOT SCHEDULED FOR INSERTION OR DELETION are processed here. |
||
| 791 | 425 | $oid = spl_object_hash($entity); |
|
| 792 | |||
| 793 | 425 | if ( ! isset($this->entityInsertions[$oid]) && ! isset($this->entityDeletions[$oid]) && isset($this->entityStates[$oid])) { |
|
| 794 | 446 | $this->computeChangeSet($class, $entity); |
|
| 795 | } |
||
| 796 | } |
||
| 797 | } |
||
| 798 | 1004 | } |
|
| 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 | 864 | private function computeAssociationChanges($assoc, $value) |
|
| 812 | { |
||
| 813 | 864 | if ($value instanceof Proxy && ! $value->__isInitialized__) { |
|
| 814 | 27 | return; |
|
| 815 | } |
||
| 816 | |||
| 817 | 863 | if ($value instanceof PersistentCollection && $value->isDirty()) { |
|
| 818 | 532 | $coid = spl_object_hash($value); |
|
| 819 | |||
| 820 | 532 | $this->collectionUpdates[$coid] = $value; |
|
| 821 | 532 | $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 | 863 | $unwrappedValue = ($assoc['type'] & ClassMetadata::TO_ONE) ? array($value) : $value->unwrap(); |
|
| 828 | 863 | $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); |
|
| 829 | |||
| 830 | 863 | foreach ($unwrappedValue as $key => $entry) { |
|
| 831 | 721 | if (! ($entry instanceof $targetClass->name)) { |
|
| 832 | 6 | throw ORMInvalidArgumentException::invalidAssociation($targetClass, $assoc, $entry); |
|
| 833 | } |
||
| 834 | |||
| 835 | 715 | $state = $this->getEntityState($entry, self::STATE_NEW); |
|
| 836 | |||
| 837 | 715 | if ( ! ($entry instanceof $assoc['targetEntity'])) { |
|
| 838 | throw ORMException::unexpectedAssociationValue($assoc['sourceEntity'], $assoc['fieldName'], get_class($entry), $assoc['targetEntity']); |
||
| 839 | } |
||
| 840 | |||
| 841 | switch ($state) { |
||
| 842 | 715 | 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 | 709 | 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 | 709 | 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 | 712 | 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 | 855 | } |
|
| 871 | |||
| 872 | /** |
||
| 873 | * @param \Doctrine\ORM\Mapping\ClassMetadata $class |
||
| 874 | * @param object $entity |
||
| 875 | * |
||
| 876 | * @return void |
||
| 877 | */ |
||
| 878 | 1024 | private function persistNew($class, $entity) |
|
| 879 | { |
||
| 880 | 1024 | $oid = spl_object_hash($entity); |
|
| 881 | 1024 | $invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::prePersist); |
|
| 882 | |||
| 883 | 1024 | if ($invoke !== ListenersInvoker::INVOKE_NONE) { |
|
| 884 | 139 | $this->listenersInvoker->invoke($class, Events::prePersist, $entity, new LifecycleEventArgs($entity, $this->em), $invoke); |
|
| 885 | } |
||
| 886 | |||
| 887 | 1024 | $idGen = $class->idGenerator; |
|
| 888 | |||
| 889 | 1024 | if ( ! $idGen->isPostInsertGenerator()) { |
|
| 890 | 262 | $idValue = $idGen->generate($this->em, $entity); |
|
| 891 | |||
| 892 | 262 | if ( ! $idGen instanceof \Doctrine\ORM\Id\AssignedGenerator) { |
|
| 893 | 1 | $idValue = array($class->identifier[0] => $idValue); |
|
| 894 | |||
| 895 | 1 | $class->setIdentifierValues($entity, $idValue); |
|
| 896 | } |
||
| 897 | |||
| 898 | 262 | $this->entityIdentifiers[$oid] = $idValue; |
|
| 899 | } |
||
| 900 | |||
| 901 | 1024 | $this->entityStates[$oid] = self::STATE_MANAGED; |
|
| 902 | |||
| 903 | 1024 | $this->scheduleForInsert($entity); |
|
| 904 | 1024 | } |
|
| 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) |
|
| 925 | { |
||
| 926 | 16 | $oid = spl_object_hash($entity); |
|
| 927 | |||
| 928 | 16 | if ( ! isset($this->entityStates[$oid]) || $this->entityStates[$oid] != self::STATE_MANAGED) { |
|
| 929 | throw ORMInvalidArgumentException::entityNotManaged($entity); |
||
| 930 | } |
||
| 931 | |||
| 932 | // skip if change tracking is "NOTIFY" |
||
| 933 | 16 | if ($class->isChangeTrackingNotify()) { |
|
| 934 | return; |
||
| 935 | } |
||
| 936 | |||
| 937 | 16 | if ( ! $class->isInheritanceTypeNone()) { |
|
| 938 | 3 | $class = $this->em->getClassMetadata(get_class($entity)); |
|
| 939 | } |
||
| 940 | |||
| 941 | 16 | $actualData = array(); |
|
| 942 | |||
| 943 | 16 | foreach ($class->reflFields as $name => $refProp) { |
|
| 944 | 16 | if (( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) |
|
| 945 | 16 | && ($name !== $class->versionField) |
|
| 946 | 16 | && ! $class->isCollectionValuedAssociation($name)) { |
|
| 947 | 16 | $actualData[$name] = $refProp->getValue($entity); |
|
| 948 | } |
||
| 949 | } |
||
| 950 | |||
| 951 | 16 | if ( ! isset($this->originalEntityData[$oid])) { |
|
| 952 | throw new \RuntimeException('Cannot call recomputeSingleEntityChangeSet before computeChangeSet on an entity.'); |
||
| 953 | } |
||
| 954 | |||
| 955 | 16 | $originalData = $this->originalEntityData[$oid]; |
|
| 956 | 16 | $changeSet = array(); |
|
| 957 | |||
| 958 | 16 | foreach ($actualData as $propName => $actualValue) { |
|
| 959 | 16 | $orgValue = isset($originalData[$propName]) ? $originalData[$propName] : null; |
|
| 960 | |||
| 961 | 16 | if ($orgValue !== $actualValue) { |
|
| 962 | 16 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 963 | } |
||
| 964 | } |
||
| 965 | |||
| 966 | 16 | if ($changeSet) { |
|
| 967 | 7 | if (isset($this->entityChangeSets[$oid])) { |
|
| 968 | 6 | $this->entityChangeSets[$oid] = array_merge($this->entityChangeSets[$oid], $changeSet); |
|
| 969 | 1 | } else if ( ! isset($this->entityInsertions[$oid])) { |
|
| 970 | 1 | $this->entityChangeSets[$oid] = $changeSet; |
|
| 971 | 1 | $this->entityUpdates[$oid] = $entity; |
|
| 972 | } |
||
| 973 | 7 | $this->originalEntityData[$oid] = $actualData; |
|
| 974 | } |
||
| 975 | 16 | } |
|
| 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 | 1003 | private function executeInserts($class) |
|
| 985 | { |
||
| 986 | 1003 | $entities = array(); |
|
| 987 | 1003 | $className = $class->name; |
|
| 988 | 1003 | $persister = $this->getEntityPersister($className); |
|
| 989 | 1003 | $invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::postPersist); |
|
| 990 | |||
| 991 | 1003 | foreach ($this->entityInsertions as $oid => $entity) { |
|
| 992 | |||
| 993 | 1003 | if ($this->em->getClassMetadata(get_class($entity))->name !== $className) { |
|
| 994 | 854 | continue; |
|
| 995 | } |
||
| 996 | |||
| 997 | 1003 | $persister->addInsert($entity); |
|
| 998 | |||
| 999 | 1003 | unset($this->entityInsertions[$oid]); |
|
| 1000 | |||
| 1001 | 1003 | if ($invoke !== ListenersInvoker::INVOKE_NONE) { |
|
| 1002 | 1003 | $entities[] = $entity; |
|
| 1003 | } |
||
| 1004 | } |
||
| 1005 | |||
| 1006 | 1003 | $postInsertIds = $persister->executeInserts(); |
|
| 1007 | |||
| 1008 | 1003 | if ($postInsertIds) { |
|
| 1009 | // Persister returned post-insert IDs |
||
| 1010 | 919 | foreach ($postInsertIds as $postInsertId) { |
|
| 1011 | 919 | $id = $postInsertId['generatedId']; |
|
| 1012 | 919 | $entity = $postInsertId['entity']; |
|
| 1013 | 919 | $oid = spl_object_hash($entity); |
|
| 1014 | 919 | $idField = $class->identifier[0]; |
|
| 1015 | |||
| 1016 | 919 | $class->reflFields[$idField]->setValue($entity, $id); |
|
| 1017 | |||
| 1018 | 919 | $this->entityIdentifiers[$oid] = array($idField => $id); |
|
| 1019 | 919 | $this->entityStates[$oid] = self::STATE_MANAGED; |
|
| 1020 | 919 | $this->originalEntityData[$oid][$idField] = $id; |
|
| 1021 | |||
| 1022 | 919 | $this->addToIdentityMap($entity); |
|
| 1023 | } |
||
| 1024 | } |
||
| 1025 | |||
| 1026 | 1003 | foreach ($entities as $entity) { |
|
| 1027 | 135 | $this->listenersInvoker->invoke($class, Events::postPersist, $entity, new LifecycleEventArgs($entity, $this->em), $invoke); |
|
| 1028 | } |
||
| 1029 | 1003 | } |
|
| 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) |
|
| 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 | 1007 | private function getCommitOrder(array $entityChangeSet = null) |
|
| 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 | 1025 | public function scheduleForInsert($entity) |
|
| 1223 | |||
| 1224 | /** |
||
| 1225 | * Checks whether an entity is scheduled for insertion. |
||
| 1226 | * |
||
| 1227 | * @param object $entity |
||
| 1228 | * |
||
| 1229 | * @return boolean |
||
| 1230 | */ |
||
| 1231 | 630 | public function isScheduledForInsert($entity) |
|
| 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) |
|
| 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) |
|
| 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) |
||
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
||
| 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 | 1086 | public function addToIdentityMap($entity) |
|
| 1417 | |||
| 1418 | /** |
||
| 1419 | * Gets the state of an entity with regard to the current unit of work. |
||
| 1420 | * |
||
| 1421 | * @param object $entity |
||
| 1422 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 1423 | * This parameter can be set to improve performance of entity state detection |
||
| 1424 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 1425 | * is either known or does not matter for the caller of the method. |
||
| 1426 | * |
||
| 1427 | * @return int The entity state. |
||
| 1428 | */ |
||
| 1429 | 1038 | public function getEntityState($entity, $assume = null) |
|
| 1498 | |||
| 1499 | /** |
||
| 1500 | * INTERNAL: |
||
| 1501 | * Removes an entity from the identity map. This effectively detaches the |
||
| 1502 | * entity from the persistence management of Doctrine. |
||
| 1503 | * |
||
| 1504 | * @ignore |
||
| 1505 | * |
||
| 1506 | * @param object $entity |
||
| 1507 | * |
||
| 1508 | * @return boolean |
||
| 1509 | * |
||
| 1510 | * @throws ORMInvalidArgumentException |
||
| 1511 | */ |
||
| 1512 | 77 | public function removeFromIdentityMap($entity) |
|
| 1535 | |||
| 1536 | /** |
||
| 1537 | * INTERNAL: |
||
| 1538 | * Gets an entity in the identity map by its identifier hash. |
||
| 1539 | * |
||
| 1540 | * @ignore |
||
| 1541 | * |
||
| 1542 | * @param string $idHash |
||
| 1543 | * @param string $rootClassName |
||
| 1544 | * |
||
| 1545 | * @return object |
||
| 1546 | */ |
||
| 1547 | public function getByIdHash($idHash, $rootClassName) |
||
| 1551 | |||
| 1552 | /** |
||
| 1553 | * INTERNAL: |
||
| 1554 | * Tries to get an entity by its identifier hash. If no entity is found for |
||
| 1555 | * the given hash, FALSE is returned. |
||
| 1556 | * |
||
| 1557 | * @ignore |
||
| 1558 | * |
||
| 1559 | * @param mixed $idHash (must be possible to cast it to string) |
||
| 1560 | * @param string $rootClassName |
||
| 1561 | * |
||
| 1562 | * @return object|bool The found entity or FALSE. |
||
| 1563 | */ |
||
| 1564 | 34 | public function tryGetByIdHash($idHash, $rootClassName) |
|
| 1572 | |||
| 1573 | /** |
||
| 1574 | * Checks whether an entity is registered in the identity map of this UnitOfWork. |
||
| 1575 | * |
||
| 1576 | * @param object $entity |
||
| 1577 | * |
||
| 1578 | * @return boolean |
||
| 1579 | */ |
||
| 1580 | 213 | public function isInIdentityMap($entity) |
|
| 1597 | |||
| 1598 | /** |
||
| 1599 | * INTERNAL: |
||
| 1600 | * Checks whether an identifier hash exists in the identity map. |
||
| 1601 | * |
||
| 1602 | * @ignore |
||
| 1603 | * |
||
| 1604 | * @param string $idHash |
||
| 1605 | * @param string $rootClassName |
||
| 1606 | * |
||
| 1607 | * @return boolean |
||
| 1608 | */ |
||
| 1609 | public function containsIdHash($idHash, $rootClassName) |
||
| 1613 | |||
| 1614 | /** |
||
| 1615 | * Persists an entity as part of the current unit of work. |
||
| 1616 | * |
||
| 1617 | * @param object $entity The entity to persist. |
||
| 1618 | * |
||
| 1619 | * @return void |
||
| 1620 | */ |
||
| 1621 | 1021 | public function persist($entity) |
|
| 1627 | |||
| 1628 | /** |
||
| 1629 | * Persists an entity as part of the current unit of work. |
||
| 1630 | * |
||
| 1631 | * This method is internally called during persist() cascades as it tracks |
||
| 1632 | * the already visited entities to prevent infinite recursions. |
||
| 1633 | * |
||
| 1634 | * @param object $entity The entity to persist. |
||
| 1635 | * @param array $visited The already visited entities. |
||
| 1636 | * |
||
| 1637 | * @return void |
||
| 1638 | * |
||
| 1639 | * @throws ORMInvalidArgumentException |
||
| 1640 | * @throws UnexpectedValueException |
||
| 1641 | */ |
||
| 1642 | 1021 | private function doPersist($entity, array &$visited) |
|
| 1690 | |||
| 1691 | /** |
||
| 1692 | * Deletes an entity as part of the current unit of work. |
||
| 1693 | * |
||
| 1694 | * @param object $entity The entity to remove. |
||
| 1695 | * |
||
| 1696 | * @return void |
||
| 1697 | */ |
||
| 1698 | 65 | public function remove($entity) |
|
| 1704 | |||
| 1705 | /** |
||
| 1706 | * Deletes an entity as part of the current unit of work. |
||
| 1707 | * |
||
| 1708 | * This method is internally called during delete() cascades as it tracks |
||
| 1709 | * the already visited entities to prevent infinite recursions. |
||
| 1710 | * |
||
| 1711 | * @param object $entity The entity to delete. |
||
| 1712 | * @param array $visited The map of the already visited entities. |
||
| 1713 | * |
||
| 1714 | * @return void |
||
| 1715 | * |
||
| 1716 | * @throws ORMInvalidArgumentException If the instance is a detached entity. |
||
| 1717 | * @throws UnexpectedValueException |
||
| 1718 | */ |
||
| 1719 | 65 | private function doRemove($entity, array &$visited) |
|
| 1759 | |||
| 1760 | /** |
||
| 1761 | * Merges the state of the given detached entity into this UnitOfWork. |
||
| 1762 | * |
||
| 1763 | * @param object $entity |
||
| 1764 | * |
||
| 1765 | * @return object The managed copy of the entity. |
||
| 1766 | * |
||
| 1767 | * @throws OptimisticLockException If the entity uses optimistic locking through a version |
||
| 1768 | * attribute and the version check against the managed copy fails. |
||
| 1769 | * |
||
| 1770 | * @todo Require active transaction!? OptimisticLockException may result in undefined state!? |
||
| 1771 | */ |
||
| 1772 | 40 | public function merge($entity) |
|
| 1778 | |||
| 1779 | /** |
||
| 1780 | * Executes a merge operation on an entity. |
||
| 1781 | * |
||
| 1782 | * @param object $entity |
||
| 1783 | * @param array $visited |
||
| 1784 | * @param object|null $prevManagedCopy |
||
| 1785 | * @param array|null $assoc |
||
| 1786 | * |
||
| 1787 | * @return object The managed copy of the entity. |
||
| 1788 | * |
||
| 1789 | * @throws OptimisticLockException If the entity uses optimistic locking through a version |
||
| 1790 | * attribute and the version check against the managed copy fails. |
||
| 1791 | * @throws ORMInvalidArgumentException If the entity instance is NEW. |
||
| 1792 | * @throws EntityNotFoundException |
||
| 1793 | */ |
||
| 1794 | 40 | private function doMerge($entity, array &$visited, $prevManagedCopy = null, array $assoc = []) |
|
| 1896 | |||
| 1897 | /** |
||
| 1898 | * Tests if an entity is loaded - must either be a loaded proxy or not a proxy |
||
| 1899 | * |
||
| 1900 | * @param object $entity |
||
| 1901 | * |
||
| 1902 | * @return bool |
||
| 1903 | */ |
||
| 1904 | 38 | private function isLoaded($entity) |
|
| 1908 | |||
| 1909 | /** |
||
| 1910 | * Sets/adds associated managed copies into the previous entity's association field |
||
| 1911 | * |
||
| 1912 | * @param object $entity |
||
| 1913 | * @param array $association |
||
| 1914 | * @param object $previousManagedCopy |
||
| 1915 | * @param object $managedCopy |
||
| 1916 | * |
||
| 1917 | * @return void |
||
| 1918 | */ |
||
| 1919 | 6 | private function updateAssociationWithMergedEntity($entity, array $association, $previousManagedCopy, $managedCopy) |
|
| 1939 | |||
| 1940 | /** |
||
| 1941 | * Detaches an entity from the persistence management. It's persistence will |
||
| 1942 | * no longer be managed by Doctrine. |
||
| 1943 | * |
||
| 1944 | * @param object $entity The entity to detach. |
||
| 1945 | * |
||
| 1946 | * @return void |
||
| 1947 | */ |
||
| 1948 | 12 | public function detach($entity) |
|
| 1954 | |||
| 1955 | /** |
||
| 1956 | * Executes a detach operation on the given entity. |
||
| 1957 | * |
||
| 1958 | * @param object $entity |
||
| 1959 | * @param array $visited |
||
| 1960 | * @param boolean $noCascade if true, don't cascade detach operation. |
||
| 1961 | * |
||
| 1962 | * @return void |
||
| 1963 | */ |
||
| 1964 | 15 | private function doDetach($entity, array &$visited, $noCascade = false) |
|
| 1998 | |||
| 1999 | /** |
||
| 2000 | * Refreshes the state of the given entity from the database, overwriting |
||
| 2001 | * any local, unpersisted changes. |
||
| 2002 | * |
||
| 2003 | * @param object $entity The entity to refresh. |
||
| 2004 | * |
||
| 2005 | * @return void |
||
| 2006 | * |
||
| 2007 | * @throws InvalidArgumentException If the entity is not MANAGED. |
||
| 2008 | */ |
||
| 2009 | 17 | public function refresh($entity) |
|
| 2015 | |||
| 2016 | /** |
||
| 2017 | * Executes a refresh operation on an entity. |
||
| 2018 | * |
||
| 2019 | * @param object $entity The entity to refresh. |
||
| 2020 | * @param array $visited The already visited entities during cascades. |
||
| 2021 | * |
||
| 2022 | * @return void |
||
| 2023 | * |
||
| 2024 | * @throws ORMInvalidArgumentException If the entity is not MANAGED. |
||
| 2025 | */ |
||
| 2026 | 17 | private function doRefresh($entity, array &$visited) |
|
| 2049 | |||
| 2050 | /** |
||
| 2051 | * Cascades a refresh operation to associated entities. |
||
| 2052 | * |
||
| 2053 | * @param object $entity |
||
| 2054 | * @param array $visited |
||
| 2055 | * |
||
| 2056 | * @return void |
||
| 2057 | */ |
||
| 2058 | 17 | private function cascadeRefresh($entity, array &$visited) |
|
| 2092 | |||
| 2093 | /** |
||
| 2094 | * Cascades a detach operation to associated entities. |
||
| 2095 | * |
||
| 2096 | * @param object $entity |
||
| 2097 | * @param array $visited |
||
| 2098 | * |
||
| 2099 | * @return void |
||
| 2100 | */ |
||
| 2101 | 13 | private function cascadeDetach($entity, array &$visited) |
|
| 2135 | |||
| 2136 | /** |
||
| 2137 | * Cascades a merge operation to associated entities. |
||
| 2138 | * |
||
| 2139 | * @param object $entity |
||
| 2140 | * @param object $managedCopy |
||
| 2141 | * @param array $visited |
||
| 2142 | * |
||
| 2143 | * @return void |
||
| 2144 | */ |
||
| 2145 | 38 | private function cascadeMerge($entity, $managedCopy, array &$visited) |
|
| 2175 | |||
| 2176 | /** |
||
| 2177 | * Cascades the save operation to associated entities. |
||
| 2178 | * |
||
| 2179 | * @param object $entity |
||
| 2180 | * @param array $visited |
||
| 2181 | * |
||
| 2182 | * @return void |
||
| 2183 | */ |
||
| 2184 | 1021 | private function cascadePersist($entity, array &$visited) |
|
| 2235 | |||
| 2236 | /** |
||
| 2237 | * Cascades the delete operation to associated entities. |
||
| 2238 | * |
||
| 2239 | * @param object $entity |
||
| 2240 | * @param array $visited |
||
| 2241 | * |
||
| 2242 | * @return void |
||
| 2243 | */ |
||
| 2244 | 65 | private function cascadeRemove($entity, array &$visited) |
|
| 2284 | |||
| 2285 | /** |
||
| 2286 | * Acquire a lock on the given entity. |
||
| 2287 | * |
||
| 2288 | * @param object $entity |
||
| 2289 | * @param int $lockMode |
||
| 2290 | * @param int $lockVersion |
||
| 2291 | * |
||
| 2292 | * @return void |
||
| 2293 | * |
||
| 2294 | * @throws ORMInvalidArgumentException |
||
| 2295 | * @throws TransactionRequiredException |
||
| 2296 | * @throws OptimisticLockException |
||
| 2297 | */ |
||
| 2298 | 11 | public function lock($entity, $lockMode, $lockVersion = null) |
|
| 2351 | |||
| 2352 | /** |
||
| 2353 | * Gets the CommitOrderCalculator used by the UnitOfWork to order commits. |
||
| 2354 | * |
||
| 2355 | * @return \Doctrine\ORM\Internal\CommitOrderCalculator |
||
| 2356 | */ |
||
| 2357 | 1007 | public function getCommitOrderCalculator() |
|
| 2361 | |||
| 2362 | /** |
||
| 2363 | * Clears the UnitOfWork. |
||
| 2364 | * |
||
| 2365 | * @param string|null $entityName if given, only entities of this type will get detached. |
||
| 2366 | * |
||
| 2367 | * @return void |
||
| 2368 | */ |
||
| 2369 | 1218 | public function clear($entityName = null) |
|
| 2396 | |||
| 2397 | /** |
||
| 2398 | * INTERNAL: |
||
| 2399 | * Schedules an orphaned entity for removal. The remove() operation will be |
||
| 2400 | * invoked on that entity at the beginning of the next commit of this |
||
| 2401 | * UnitOfWork. |
||
| 2402 | * |
||
| 2403 | * @ignore |
||
| 2404 | * |
||
| 2405 | * @param object $entity |
||
| 2406 | * |
||
| 2407 | * @return void |
||
| 2408 | */ |
||
| 2409 | 17 | public function scheduleOrphanRemoval($entity) |
|
| 2413 | |||
| 2414 | /** |
||
| 2415 | * INTERNAL: |
||
| 2416 | * Cancels a previously scheduled orphan removal. |
||
| 2417 | * |
||
| 2418 | * @ignore |
||
| 2419 | * |
||
| 2420 | * @param object $entity |
||
| 2421 | * |
||
| 2422 | * @return void |
||
| 2423 | */ |
||
| 2424 | 112 | public function cancelOrphanRemoval($entity) |
|
| 2428 | |||
| 2429 | /** |
||
| 2430 | * INTERNAL: |
||
| 2431 | * Schedules a complete collection for removal when this UnitOfWork commits. |
||
| 2432 | * |
||
| 2433 | * @param PersistentCollection $coll |
||
| 2434 | * |
||
| 2435 | * @return void |
||
| 2436 | */ |
||
| 2437 | 13 | public function scheduleCollectionDeletion(PersistentCollection $coll) |
|
| 2447 | |||
| 2448 | /** |
||
| 2449 | * @param PersistentCollection $coll |
||
| 2450 | * |
||
| 2451 | * @return bool |
||
| 2452 | */ |
||
| 2453 | public function isCollectionScheduledForDeletion(PersistentCollection $coll) |
||
| 2457 | |||
| 2458 | /** |
||
| 2459 | * @param ClassMetadata $class |
||
| 2460 | * |
||
| 2461 | * @return \Doctrine\Common\Persistence\ObjectManagerAware|object |
||
| 2462 | */ |
||
| 2463 | 669 | private function newInstance($class) |
|
| 2473 | |||
| 2474 | /** |
||
| 2475 | * INTERNAL: |
||
| 2476 | * Creates an entity. Used for reconstitution of persistent entities. |
||
| 2477 | * |
||
| 2478 | * Internal note: Highly performance-sensitive method. |
||
| 2479 | * |
||
| 2480 | * @ignore |
||
| 2481 | * |
||
| 2482 | * @param string $className The name of the entity class. |
||
| 2483 | * @param array $data The data for the entity. |
||
| 2484 | * @param array $hints Any hints to account for during reconstitution/lookup of the entity. |
||
| 2485 | * |
||
| 2486 | * @return object The managed entity instance. |
||
| 2487 | * |
||
| 2488 | * @todo Rename: getOrCreateEntity |
||
| 2489 | */ |
||
| 2490 | 808 | public function createEntity($className, array $data, &$hints = array()) |
|
| 2774 | |||
| 2775 | /** |
||
| 2776 | * @return void |
||
| 2777 | */ |
||
| 2778 | 865 | public function triggerEagerLoads() |
|
| 2800 | |||
| 2801 | /** |
||
| 2802 | * Initializes (loads) an uninitialized persistent collection of an entity. |
||
| 2803 | * |
||
| 2804 | * @param \Doctrine\ORM\PersistentCollection $collection The collection to initialize. |
||
| 2805 | * |
||
| 2806 | * @return void |
||
| 2807 | * |
||
| 2808 | * @todo Maybe later move to EntityManager#initialize($proxyOrCollection). See DDC-733. |
||
| 2809 | */ |
||
| 2810 | 143 | public function loadCollection(PersistentCollection $collection) |
|
| 2827 | |||
| 2828 | /** |
||
| 2829 | * Gets the identity map of the UnitOfWork. |
||
| 2830 | * |
||
| 2831 | * @return array |
||
| 2832 | */ |
||
| 2833 | 2 | public function getIdentityMap() |
|
| 2837 | |||
| 2838 | /** |
||
| 2839 | * Gets the original data of an entity. The original data is the data that was |
||
| 2840 | * present at the time the entity was reconstituted from the database. |
||
| 2841 | * |
||
| 2842 | * @param object $entity |
||
| 2843 | * |
||
| 2844 | * @return array |
||
| 2845 | */ |
||
| 2846 | 115 | public function getOriginalEntityData($entity) |
|
| 2854 | |||
| 2855 | /** |
||
| 2856 | * @ignore |
||
| 2857 | * |
||
| 2858 | * @param object $entity |
||
| 2859 | * @param array $data |
||
| 2860 | * |
||
| 2861 | * @return void |
||
| 2862 | */ |
||
| 2863 | public function setOriginalEntityData($entity, array $data) |
||
| 2867 | |||
| 2868 | /** |
||
| 2869 | * INTERNAL: |
||
| 2870 | * Sets a property value of the original data array of an entity. |
||
| 2871 | * |
||
| 2872 | * @ignore |
||
| 2873 | * |
||
| 2874 | * @param string $oid |
||
| 2875 | * @param string $property |
||
| 2876 | * @param mixed $value |
||
| 2877 | * |
||
| 2878 | * @return void |
||
| 2879 | */ |
||
| 2880 | 313 | public function setOriginalEntityProperty($oid, $property, $value) |
|
| 2884 | |||
| 2885 | /** |
||
| 2886 | * Gets the identifier of an entity. |
||
| 2887 | * The returned value is always an array of identifier values. If the entity |
||
| 2888 | * has a composite identifier then the identifier values are in the same |
||
| 2889 | * order as the identifier field names as returned by ClassMetadata#getIdentifierFieldNames(). |
||
| 2890 | * |
||
| 2891 | * @param object $entity |
||
| 2892 | * |
||
| 2893 | * @return array The identifier values. |
||
| 2894 | */ |
||
| 2895 | 843 | public function getEntityIdentifier($entity) |
|
| 2899 | |||
| 2900 | /** |
||
| 2901 | * Processes an entity instance to extract their identifier values. |
||
| 2902 | * |
||
| 2903 | * @param object $entity The entity instance. |
||
| 2904 | * |
||
| 2905 | * @return mixed A scalar value. |
||
| 2906 | * |
||
| 2907 | * @throws \Doctrine\ORM\ORMInvalidArgumentException |
||
| 2908 | */ |
||
| 2909 | 126 | public function getSingleIdentifierValue($entity) |
|
| 2923 | |||
| 2924 | /** |
||
| 2925 | * Tries to find an entity with the given identifier in the identity map of |
||
| 2926 | * this UnitOfWork. |
||
| 2927 | * |
||
| 2928 | * @param mixed $id The entity identifier to look for. |
||
| 2929 | * @param string $rootClassName The name of the root class of the mapped entity hierarchy. |
||
| 2930 | * |
||
| 2931 | * @return object|bool Returns the entity with the specified identifier if it exists in |
||
| 2932 | * this UnitOfWork, FALSE otherwise. |
||
| 2933 | */ |
||
| 2934 | 522 | public function tryGetById($id, $rootClassName) |
|
| 2942 | |||
| 2943 | /** |
||
| 2944 | * Schedules an entity for dirty-checking at commit-time. |
||
| 2945 | * |
||
| 2946 | * @param object $entity The entity to schedule for dirty-checking. |
||
| 2947 | * |
||
| 2948 | * @return void |
||
| 2949 | * |
||
| 2950 | * @todo Rename: scheduleForSynchronization |
||
| 2951 | */ |
||
| 2952 | 5 | public function scheduleForDirtyCheck($entity) |
|
| 2958 | |||
| 2959 | /** |
||
| 2960 | * Checks whether the UnitOfWork has any pending insertions. |
||
| 2961 | * |
||
| 2962 | * @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
||
| 2963 | */ |
||
| 2964 | public function hasPendingInsertions() |
||
| 2968 | |||
| 2969 | /** |
||
| 2970 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
||
| 2971 | * number of entities in the identity map. |
||
| 2972 | * |
||
| 2973 | * @return integer |
||
| 2974 | */ |
||
| 2975 | 1 | public function size() |
|
| 2981 | |||
| 2982 | /** |
||
| 2983 | * Gets the EntityPersister for an Entity. |
||
| 2984 | * |
||
| 2985 | * @param string $entityName The name of the Entity. |
||
| 2986 | * |
||
| 2987 | * @return \Doctrine\ORM\Persisters\Entity\EntityPersister |
||
| 2988 | */ |
||
| 2989 | 1069 | public function getEntityPersister($entityName) |
|
| 3025 | |||
| 3026 | /** |
||
| 3027 | * Gets a collection persister for a collection-valued association. |
||
| 3028 | * |
||
| 3029 | * @param array $association |
||
| 3030 | * |
||
| 3031 | * @return \Doctrine\ORM\Persisters\Collection\CollectionPersister |
||
| 3032 | */ |
||
| 3033 | 569 | public function getCollectionPersister(array $association) |
|
| 3058 | |||
| 3059 | /** |
||
| 3060 | * INTERNAL: |
||
| 3061 | * Registers an entity as managed. |
||
| 3062 | * |
||
| 3063 | * @param object $entity The entity. |
||
| 3064 | * @param array $id The identifier values. |
||
| 3065 | * @param array $data The original entity data. |
||
| 3066 | * |
||
| 3067 | * @return void |
||
| 3068 | */ |
||
| 3069 | 202 | public function registerManaged($entity, array $id, array $data) |
|
| 3083 | |||
| 3084 | /** |
||
| 3085 | * INTERNAL: |
||
| 3086 | * Clears the property changeset of the entity with the given OID. |
||
| 3087 | * |
||
| 3088 | * @param string $oid The entity's OID. |
||
| 3089 | * |
||
| 3090 | * @return void |
||
| 3091 | */ |
||
| 3092 | public function clearEntityChangeSet($oid) |
||
| 3096 | |||
| 3097 | /* PropertyChangedListener implementation */ |
||
| 3098 | |||
| 3099 | /** |
||
| 3100 | * Notifies this UnitOfWork of a property change in an entity. |
||
| 3101 | * |
||
| 3102 | * @param object $entity The entity that owns the property. |
||
| 3103 | * @param string $propertyName The name of the property that changed. |
||
| 3104 | * @param mixed $oldValue The old value of the property. |
||
| 3105 | * @param mixed $newValue The new value of the property. |
||
| 3106 | * |
||
| 3107 | * @return void |
||
| 3108 | */ |
||
| 3109 | 3 | public function propertyChanged($entity, $propertyName, $oldValue, $newValue) |
|
| 3127 | |||
| 3128 | /** |
||
| 3129 | * Gets the currently scheduled entity insertions in this UnitOfWork. |
||
| 3130 | * |
||
| 3131 | * @return array |
||
| 3132 | */ |
||
| 3133 | 2 | public function getScheduledEntityInsertions() |
|
| 3137 | |||
| 3138 | /** |
||
| 3139 | * Gets the currently scheduled entity updates in this UnitOfWork. |
||
| 3140 | * |
||
| 3141 | * @return array |
||
| 3142 | */ |
||
| 3143 | 2 | public function getScheduledEntityUpdates() |
|
| 3147 | |||
| 3148 | /** |
||
| 3149 | * Gets the currently scheduled entity deletions in this UnitOfWork. |
||
| 3150 | * |
||
| 3151 | * @return array |
||
| 3152 | */ |
||
| 3153 | 1 | public function getScheduledEntityDeletions() |
|
| 3157 | |||
| 3158 | /** |
||
| 3159 | * Gets the currently scheduled complete collection deletions |
||
| 3160 | * |
||
| 3161 | * @return array |
||
| 3162 | */ |
||
| 3163 | 1 | public function getScheduledCollectionDeletions() |
|
| 3167 | |||
| 3168 | /** |
||
| 3169 | * Gets the currently scheduled collection inserts, updates and deletes. |
||
| 3170 | * |
||
| 3171 | * @return array |
||
| 3172 | */ |
||
| 3173 | public function getScheduledCollectionUpdates() |
||
| 3177 | |||
| 3178 | /** |
||
| 3179 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 3180 | * |
||
| 3181 | * @param object $obj |
||
| 3182 | * |
||
| 3183 | * @return void |
||
| 3184 | */ |
||
| 3185 | 2 | public function initializeObject($obj) |
|
| 3197 | |||
| 3198 | /** |
||
| 3199 | * Helper method to show an object as string. |
||
| 3200 | * |
||
| 3201 | * @param object $obj |
||
| 3202 | * |
||
| 3203 | * @return string |
||
| 3204 | */ |
||
| 3205 | 1 | private static function objToStr($obj) |
|
| 3209 | |||
| 3210 | /** |
||
| 3211 | * Marks an entity as read-only so that it will not be considered for updates during UnitOfWork#commit(). |
||
| 3212 | * |
||
| 3213 | * This operation cannot be undone as some parts of the UnitOfWork now keep gathering information |
||
| 3214 | * on this object that might be necessary to perform a correct update. |
||
| 3215 | * |
||
| 3216 | * @param object $object |
||
| 3217 | * |
||
| 3218 | * @return void |
||
| 3219 | * |
||
| 3220 | * @throws ORMInvalidArgumentException |
||
| 3221 | */ |
||
| 3222 | 6 | public function markReadOnly($object) |
|
| 3230 | |||
| 3231 | /** |
||
| 3232 | * Is this entity read only? |
||
| 3233 | * |
||
| 3234 | * @param object $object |
||
| 3235 | * |
||
| 3236 | * @return bool |
||
| 3237 | * |
||
| 3238 | * @throws ORMInvalidArgumentException |
||
| 3239 | */ |
||
| 3240 | 3 | public function isReadOnly($object) |
|
| 3248 | |||
| 3249 | /** |
||
| 3250 | * Perform whatever processing is encapsulated here after completion of the transaction. |
||
| 3251 | */ |
||
| 3252 | 1002 | private function afterTransactionComplete() |
|
| 3258 | |||
| 3259 | /** |
||
| 3260 | * Perform whatever processing is encapsulated here after completion of the rolled-back. |
||
| 3261 | */ |
||
| 3262 | private function afterTransactionRolledBack() |
||
| 3268 | |||
| 3269 | /** |
||
| 3270 | * Performs an action after the transaction. |
||
| 3271 | * |
||
| 3272 | * @param callable $callback |
||
| 3273 | */ |
||
| 3274 | 1007 | private function performCallbackOnCachedPersister(callable $callback) |
|
| 3286 | |||
| 3287 | 1011 | private function dispatchOnFlushEvent() |
|
| 3293 | |||
| 3294 | 1006 | private function dispatchPostFlushEvent() |
|
| 3300 | |||
| 3301 | /** |
||
| 3302 | * Verifies if two given entities actually are the same based on identifier comparison |
||
| 3303 | * |
||
| 3304 | * @param object $entity1 |
||
| 3305 | * @param object $entity2 |
||
| 3306 | * |
||
| 3307 | * @return bool |
||
| 3308 | */ |
||
| 3309 | 14 | private function isIdentifierEquals($entity1, $entity2) |
|
| 3333 | |||
| 3334 | /** |
||
| 3335 | * @param object $entity |
||
| 3336 | * @param object $managedCopy |
||
| 3337 | * |
||
| 3338 | * @throws ORMException |
||
| 3339 | * @throws OptimisticLockException |
||
| 3340 | * @throws TransactionRequiredException |
||
| 3341 | */ |
||
| 3342 | 30 | private function mergeEntityStateIntoManagedCopy($entity, $managedCopy) |
|
| 3435 | |||
| 3436 | /** |
||
| 3437 | * This method called by hydrators, and indicates that hydrator totally completed current hydration cycle. |
||
| 3438 | * Unit of work able to fire deferred events, related to loading events here. |
||
| 3439 | * |
||
| 3440 | * @internal should be called internally from object hydrators |
||
| 3441 | */ |
||
| 3442 | 876 | public function hydrationComplete() |
|
| 3446 | |||
| 3447 | /** |
||
| 3448 | * @param string $entityName |
||
| 3449 | */ |
||
| 3450 | 3 | private function clearIdentityMapForEntityName($entityName) |
|
| 3462 | |||
| 3463 | /** |
||
| 3464 | * @param string $entityName |
||
| 3465 | */ |
||
| 3466 | 3 | private function clearEntityInsertionsForEntityName($entityName) |
|
| 3475 | } |
||
| 3476 |
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.