Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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  | 
            ||
| 64 | class UnitOfWork implements PropertyChangedListener  | 
            ||
| 65 | { | 
            ||
| 66 | /**  | 
            ||
| 67 | * An entity is in MANAGED state when its persistence is managed by an EntityManager.  | 
            ||
| 68 | */  | 
            ||
| 69 | const STATE_MANAGED = 1;  | 
            ||
| 70 | |||
| 71 | /**  | 
            ||
| 72 | * An entity is new if it has just been instantiated (i.e. using the "new" operator)  | 
            ||
| 73 | * and is not (yet) managed by an EntityManager.  | 
            ||
| 74 | */  | 
            ||
| 75 | const STATE_NEW = 2;  | 
            ||
| 76 | |||
| 77 | /**  | 
            ||
| 78 | * A detached entity is an instance with persistent state and identity that is not  | 
            ||
| 79 | * (or no longer) associated with an EntityManager (and a UnitOfWork).  | 
            ||
| 80 | */  | 
            ||
| 81 | const STATE_DETACHED = 3;  | 
            ||
| 82 | |||
| 83 | /**  | 
            ||
| 84 | * A removed entity instance is an instance with a persistent identity,  | 
            ||
| 85 | * associated with an EntityManager, whose persistent state will be deleted  | 
            ||
| 86 | * on commit.  | 
            ||
| 87 | */  | 
            ||
| 88 | const STATE_REMOVED = 4;  | 
            ||
| 89 | |||
| 90 | /**  | 
            ||
| 91 | * Hint used to collect all primary keys of associated entities during hydration  | 
            ||
| 92 | * and execute it in a dedicated query afterwards  | 
            ||
| 93 | * @see https://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html?highlight=eager#temporarily-change-fetch-mode-in-dql  | 
            ||
| 94 | */  | 
            ||
| 95 | const HINT_DEFEREAGERLOAD = 'deferEagerLoad';  | 
            ||
| 96 | |||
| 97 | /**  | 
            ||
| 98 | * The identity map that holds references to all managed entities that have  | 
            ||
| 99 | * an identity. The entities are grouped by their class name.  | 
            ||
| 100 | * Since all classes in a hierarchy must share the same identifier set,  | 
            ||
| 101 | * we always take the root class name of the hierarchy.  | 
            ||
| 102 | *  | 
            ||
| 103 | * @var array  | 
            ||
| 104 | */  | 
            ||
| 105 | private $identityMap = [];  | 
            ||
| 106 | |||
| 107 | /**  | 
            ||
| 108 | * Map of all identifiers of managed entities.  | 
            ||
| 109 | * Keys are object ids (spl_object_hash).  | 
            ||
| 110 | *  | 
            ||
| 111 | * @var array  | 
            ||
| 112 | */  | 
            ||
| 113 | private $entityIdentifiers = [];  | 
            ||
| 114 | |||
| 115 | /**  | 
            ||
| 116 | * Map of the original entity data of managed entities.  | 
            ||
| 117 | * Keys are object ids (spl_object_hash). This is used for calculating changesets  | 
            ||
| 118 | * at commit time.  | 
            ||
| 119 | *  | 
            ||
| 120 | * Internal note: Note that PHPs "copy-on-write" behavior helps a lot with memory usage.  | 
            ||
| 121 | * A value will only really be copied if the value in the entity is modified  | 
            ||
| 122 | * by the user.  | 
            ||
| 123 | *  | 
            ||
| 124 | * @var array  | 
            ||
| 125 | */  | 
            ||
| 126 | private $originalEntityData = [];  | 
            ||
| 127 | |||
| 128 | /**  | 
            ||
| 129 | * Map of entity changes. Keys are object ids (spl_object_hash).  | 
            ||
| 130 | * Filled at the beginning of a commit of the UnitOfWork and cleaned at the end.  | 
            ||
| 131 | *  | 
            ||
| 132 | * @var array  | 
            ||
| 133 | */  | 
            ||
| 134 | private $entityChangeSets = [];  | 
            ||
| 135 | |||
| 136 | /**  | 
            ||
| 137 | * The (cached) states of any known entities.  | 
            ||
| 138 | * Keys are object ids (spl_object_hash).  | 
            ||
| 139 | *  | 
            ||
| 140 | * @var array  | 
            ||
| 141 | */  | 
            ||
| 142 | private $entityStates = [];  | 
            ||
| 143 | |||
| 144 | /**  | 
            ||
| 145 | * Map of entities that are scheduled for dirty checking at commit time.  | 
            ||
| 146 | * This is only used for entities with a change tracking policy of DEFERRED_EXPLICIT.  | 
            ||
| 147 | * Keys are object ids (spl_object_hash).  | 
            ||
| 148 | *  | 
            ||
| 149 | * @var array  | 
            ||
| 150 | */  | 
            ||
| 151 | private $scheduledForSynchronization = [];  | 
            ||
| 152 | |||
| 153 | /**  | 
            ||
| 154 | * A list of all pending entity insertions.  | 
            ||
| 155 | *  | 
            ||
| 156 | * @var array  | 
            ||
| 157 | */  | 
            ||
| 158 | private $entityInsertions = [];  | 
            ||
| 159 | |||
| 160 | /**  | 
            ||
| 161 | * A list of all pending entity updates.  | 
            ||
| 162 | *  | 
            ||
| 163 | * @var array  | 
            ||
| 164 | */  | 
            ||
| 165 | private $entityUpdates = [];  | 
            ||
| 166 | |||
| 167 | /**  | 
            ||
| 168 | * Any pending extra updates that have been scheduled by persisters.  | 
            ||
| 169 | *  | 
            ||
| 170 | * @var array  | 
            ||
| 171 | */  | 
            ||
| 172 | private $extraUpdates = [];  | 
            ||
| 173 | |||
| 174 | /**  | 
            ||
| 175 | * A list of all pending entity deletions.  | 
            ||
| 176 | *  | 
            ||
| 177 | * @var array  | 
            ||
| 178 | */  | 
            ||
| 179 | private $entityDeletions = [];  | 
            ||
| 180 | |||
| 181 | /**  | 
            ||
| 182 | * All pending collection deletions.  | 
            ||
| 183 | *  | 
            ||
| 184 | * @var array  | 
            ||
| 185 | */  | 
            ||
| 186 | private $collectionDeletions = [];  | 
            ||
| 187 | |||
| 188 | /**  | 
            ||
| 189 | * All pending collection updates.  | 
            ||
| 190 | *  | 
            ||
| 191 | * @var array  | 
            ||
| 192 | */  | 
            ||
| 193 | private $collectionUpdates = [];  | 
            ||
| 194 | |||
| 195 | /**  | 
            ||
| 196 | * List of collections visited during changeset calculation on a commit-phase of a UnitOfWork.  | 
            ||
| 197 | * At the end of the UnitOfWork all these collections will make new snapshots  | 
            ||
| 198 | * of their data.  | 
            ||
| 199 | *  | 
            ||
| 200 | * @var array  | 
            ||
| 201 | */  | 
            ||
| 202 | private $visitedCollections = [];  | 
            ||
| 203 | |||
| 204 | /**  | 
            ||
| 205 | * The EntityManager that "owns" this UnitOfWork instance.  | 
            ||
| 206 | *  | 
            ||
| 207 | * @var EntityManagerInterface  | 
            ||
| 208 | */  | 
            ||
| 209 | private $em;  | 
            ||
| 210 | |||
| 211 | /**  | 
            ||
| 212 | * The entity persister instances used to persist entity instances.  | 
            ||
| 213 | *  | 
            ||
| 214 | * @var array  | 
            ||
| 215 | */  | 
            ||
| 216 | private $persisters = [];  | 
            ||
| 217 | |||
| 218 | /**  | 
            ||
| 219 | * The collection persister instances used to persist collections.  | 
            ||
| 220 | *  | 
            ||
| 221 | * @var array  | 
            ||
| 222 | */  | 
            ||
| 223 | private $collectionPersisters = [];  | 
            ||
| 224 | |||
| 225 | /**  | 
            ||
| 226 | * The EventManager used for dispatching events.  | 
            ||
| 227 | *  | 
            ||
| 228 | * @var \Doctrine\Common\EventManager  | 
            ||
| 229 | */  | 
            ||
| 230 | private $evm;  | 
            ||
| 231 | |||
| 232 | /**  | 
            ||
| 233 | * The ListenersInvoker used for dispatching events.  | 
            ||
| 234 | *  | 
            ||
| 235 | * @var \Doctrine\ORM\Event\ListenersInvoker  | 
            ||
| 236 | */  | 
            ||
| 237 | private $listenersInvoker;  | 
            ||
| 238 | |||
| 239 | /**  | 
            ||
| 240 | * The IdentifierFlattener used for manipulating identifiers  | 
            ||
| 241 | *  | 
            ||
| 242 | * @var \Doctrine\ORM\Utility\IdentifierFlattener  | 
            ||
| 243 | */  | 
            ||
| 244 | private $identifierFlattener;  | 
            ||
| 245 | |||
| 246 | /**  | 
            ||
| 247 | * Orphaned entities that are scheduled for removal.  | 
            ||
| 248 | *  | 
            ||
| 249 | * @var array  | 
            ||
| 250 | */  | 
            ||
| 251 | private $orphanRemovals = [];  | 
            ||
| 252 | |||
| 253 | /**  | 
            ||
| 254 | * Read-Only objects are never evaluated  | 
            ||
| 255 | *  | 
            ||
| 256 | * @var array  | 
            ||
| 257 | */  | 
            ||
| 258 | private $readOnlyObjects = [];  | 
            ||
| 259 | |||
| 260 | /**  | 
            ||
| 261 | * Map of Entity Class-Names and corresponding IDs that should eager loaded when requested.  | 
            ||
| 262 | *  | 
            ||
| 263 | * @var array  | 
            ||
| 264 | */  | 
            ||
| 265 | private $eagerLoadingEntities = [];  | 
            ||
| 266 | |||
| 267 | /**  | 
            ||
| 268 | * @var boolean  | 
            ||
| 269 | */  | 
            ||
| 270 | protected $hasCache = false;  | 
            ||
| 271 | |||
| 272 | /**  | 
            ||
| 273 | * Helper for handling completion of hydration  | 
            ||
| 274 | *  | 
            ||
| 275 | * @var HydrationCompleteHandler  | 
            ||
| 276 | */  | 
            ||
| 277 | private $hydrationCompleteHandler;  | 
            ||
| 278 | |||
| 279 | /**  | 
            ||
| 280 | * @var ReflectionPropertiesGetter  | 
            ||
| 281 | */  | 
            ||
| 282 | private $reflectionPropertiesGetter;  | 
            ||
| 283 | |||
| 284 | /**  | 
            ||
| 285 | * Initializes a new UnitOfWork instance, bound to the given EntityManager.  | 
            ||
| 286 | *  | 
            ||
| 287 | * @param EntityManagerInterface $em  | 
            ||
| 288 | */  | 
            ||
| 289 | 2394 | public function __construct(EntityManagerInterface $em)  | 
            |
| 290 |     { | 
            ||
| 291 | 2394 | $this->em = $em;  | 
            |
| 292 | 2394 | $this->evm = $em->getEventManager();  | 
            |
| 293 | 2394 | $this->listenersInvoker = new ListenersInvoker($em);  | 
            |
| 294 | 2394 | $this->hasCache = $em->getConfiguration()->isSecondLevelCacheEnabled();  | 
            |
| 295 | 2394 | $this->identifierFlattener = new IdentifierFlattener($this, $em->getMetadataFactory());  | 
            |
| 296 | 2394 | $this->hydrationCompleteHandler = new HydrationCompleteHandler($this->listenersInvoker, $em);  | 
            |
| 297 | 2394 | $this->reflectionPropertiesGetter = new ReflectionPropertiesGetter(new RuntimeReflectionService());  | 
            |
| 298 | 2394 | }  | 
            |
| 299 | |||
| 300 | /**  | 
            ||
| 301 | * Commits the UnitOfWork, executing all operations that have been postponed  | 
            ||
| 302 | * up to this point. The state of all managed entities will be synchronized with  | 
            ||
| 303 | * the database.  | 
            ||
| 304 | *  | 
            ||
| 305 | * The operations are executed in the following order:  | 
            ||
| 306 | *  | 
            ||
| 307 | * 1) All entity insertions  | 
            ||
| 308 | * 2) All entity updates  | 
            ||
| 309 | * 3) All collection deletions  | 
            ||
| 310 | * 4) All collection updates  | 
            ||
| 311 | * 5) All entity deletions  | 
            ||
| 312 | *  | 
            ||
| 313 | * @param null|object|array $entity  | 
            ||
| 314 | *  | 
            ||
| 315 | * @return void  | 
            ||
| 316 | *  | 
            ||
| 317 | * @throws \Exception  | 
            ||
| 318 | */  | 
            ||
| 319 | 1037 | public function commit($entity = null)  | 
            |
| 320 |     { | 
            ||
| 321 | // Raise preFlush  | 
            ||
| 322 | 1037 |         if ($this->evm->hasListeners(Events::preFlush)) { | 
            |
| 323 | 2 | $this->evm->dispatchEvent(Events::preFlush, new PreFlushEventArgs($this->em));  | 
            |
| 324 | }  | 
            ||
| 325 | |||
| 326 | // Compute changes done since last commit.  | 
            ||
| 327 | 1037 |         if ($entity === null) { | 
            |
| 328 | 1029 | $this->computeChangeSets();  | 
            |
| 329 | 16 |         } elseif (is_object($entity)) { | 
            |
| 330 | 15 | $this->computeSingleEntityChangeSet($entity);  | 
            |
| 331 | 1 |         } elseif (is_array($entity)) { | 
            |
| 332 | 1 |             foreach ($entity as $object) { | 
            |
| 333 | 1 | $this->computeSingleEntityChangeSet($object);  | 
            |
| 334 | }  | 
            ||
| 335 | }  | 
            ||
| 336 | |||
| 337 | 1034 | if ( ! ($this->entityInsertions ||  | 
            |
| 
                                                                                                    
                        
                         | 
                |||
| 338 | 168 | $this->entityDeletions ||  | 
            |
| 339 | 132 | $this->entityUpdates ||  | 
            |
| 340 | 40 | $this->collectionUpdates ||  | 
            |
| 341 | 37 | $this->collectionDeletions ||  | 
            |
| 342 | 1034 |                 $this->orphanRemovals)) { | 
            |
| 343 | 25 | $this->dispatchOnFlushEvent();  | 
            |
| 344 | 25 | $this->dispatchPostFlushEvent();  | 
            |
| 345 | |||
| 346 | 25 | return; // Nothing to do.  | 
            |
| 347 | }  | 
            ||
| 348 | |||
| 349 | 1030 |         if ($this->orphanRemovals) { | 
            |
| 350 | 16 |             foreach ($this->orphanRemovals as $orphan) { | 
            |
| 351 | 16 | $this->remove($orphan);  | 
            |
| 352 | }  | 
            ||
| 353 | }  | 
            ||
| 354 | |||
| 355 | 1030 | $this->dispatchOnFlushEvent();  | 
            |
| 356 | |||
| 357 | // Now we need a commit order to maintain referential integrity  | 
            ||
| 358 | 1030 | $commitOrder = $this->getCommitOrder();  | 
            |
| 359 | |||
| 360 | 1030 | $conn = $this->em->getConnection();  | 
            |
| 361 | 1030 | $conn->beginTransaction();  | 
            |
| 362 | |||
| 363 |         try { | 
            ||
| 364 | // Collection deletions (deletions of complete collections)  | 
            ||
| 365 | 1030 |             foreach ($this->collectionDeletions as $collectionToDelete) { | 
            |
| 366 | 19 | $this->getCollectionPersister($collectionToDelete->getMapping())->delete($collectionToDelete);  | 
            |
| 367 | }  | 
            ||
| 368 | |||
| 369 | 1030 |             if ($this->entityInsertions) { | 
            |
| 370 | 1026 |                 foreach ($commitOrder as $class) { | 
            |
| 371 | 1026 | $this->executeInserts($class);  | 
            |
| 372 | }  | 
            ||
| 373 | }  | 
            ||
| 374 | |||
| 375 | 1029 |             if ($this->entityUpdates) { | 
            |
| 376 | 118 |                 foreach ($commitOrder as $class) { | 
            |
| 377 | 118 | $this->executeUpdates($class);  | 
            |
| 378 | }  | 
            ||
| 379 | }  | 
            ||
| 380 | |||
| 381 | // Extra updates that were requested by persisters.  | 
            ||
| 382 | 1025 |             if ($this->extraUpdates) { | 
            |
| 383 | 44 | $this->executeExtraUpdates();  | 
            |
| 384 | }  | 
            ||
| 385 | |||
| 386 | // Collection updates (deleteRows, updateRows, insertRows)  | 
            ||
| 387 | 1025 |             foreach ($this->collectionUpdates as $collectionToUpdate) { | 
            |
| 388 | 534 | $this->getCollectionPersister($collectionToUpdate->getMapping())->update($collectionToUpdate);  | 
            |
| 389 | }  | 
            ||
| 390 | |||
| 391 | // Entity deletions come last and need to be in reverse commit order  | 
            ||
| 392 | 1025 |             if ($this->entityDeletions) { | 
            |
| 393 | 63 |                 for ($count = count($commitOrder), $i = $count - 1; $i >= 0 && $this->entityDeletions; --$i) { | 
            |
| 394 | 63 | $this->executeDeletions($commitOrder[$i]);  | 
            |
| 395 | }  | 
            ||
| 396 | }  | 
            ||
| 397 | |||
| 398 | 1025 | $conn->commit();  | 
            |
| 399 | 11 |         } catch (Exception $e) { | 
            |
| 400 | 11 | $this->em->close();  | 
            |
| 401 | 11 | $conn->rollBack();  | 
            |
| 402 | |||
| 403 | 11 | $this->afterTransactionRolledBack();  | 
            |
| 404 | |||
| 405 | 11 | throw $e;  | 
            |
| 406 | }  | 
            ||
| 407 | |||
| 408 | 1025 | $this->afterTransactionComplete();  | 
            |
| 409 | |||
| 410 | // Take new snapshots from visited collections  | 
            ||
| 411 | 1025 |         foreach ($this->visitedCollections as $coll) { | 
            |
| 412 | 533 | $coll->takeSnapshot();  | 
            |
| 413 | }  | 
            ||
| 414 | |||
| 415 | 1025 | $this->dispatchPostFlushEvent();  | 
            |
| 416 | |||
| 417 | // Clear up  | 
            ||
| 418 | 1024 | $this->entityInsertions =  | 
            |
| 419 | 1024 | $this->entityUpdates =  | 
            |
| 420 | 1024 | $this->entityDeletions =  | 
            |
| 421 | 1024 | $this->extraUpdates =  | 
            |
| 422 | 1024 | $this->entityChangeSets =  | 
            |
| 423 | 1024 | $this->collectionUpdates =  | 
            |
| 424 | 1024 | $this->collectionDeletions =  | 
            |
| 425 | 1024 | $this->visitedCollections =  | 
            |
| 426 | 1024 | $this->scheduledForSynchronization =  | 
            |
| 427 | 1024 | $this->orphanRemovals = [];  | 
            |
| 428 | 1024 | }  | 
            |
| 429 | |||
| 430 | /**  | 
            ||
| 431 | * Computes the changesets of all entities scheduled for insertion.  | 
            ||
| 432 | *  | 
            ||
| 433 | * @return void  | 
            ||
| 434 | */  | 
            ||
| 435 | 1036 | private function computeScheduleInsertsChangeSets()  | 
            |
| 436 |     { | 
            ||
| 437 | 1036 |         foreach ($this->entityInsertions as $entity) { | 
            |
| 438 | 1028 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 439 | |||
| 440 | 1028 | $this->computeChangeSet($class, $entity);  | 
            |
| 441 | }  | 
            ||
| 442 | 1034 | }  | 
            |
| 443 | |||
| 444 | /**  | 
            ||
| 445 | * Only flushes the given entity according to a ruleset that keeps the UoW consistent.  | 
            ||
| 446 | *  | 
            ||
| 447 | * 1. All entities scheduled for insertion, (orphan) removals and changes in collections are processed as well!  | 
            ||
| 448 | * 2. Read Only entities are skipped.  | 
            ||
| 449 | * 3. Proxies are skipped.  | 
            ||
| 450 | * 4. Only if entity is properly managed.  | 
            ||
| 451 | *  | 
            ||
| 452 | * @param object $entity  | 
            ||
| 453 | *  | 
            ||
| 454 | * @return void  | 
            ||
| 455 | *  | 
            ||
| 456 | * @throws \InvalidArgumentException  | 
            ||
| 457 | */  | 
            ||
| 458 | 16 | private function computeSingleEntityChangeSet($entity)  | 
            |
| 459 |     { | 
            ||
| 460 | 16 | $state = $this->getEntityState($entity);  | 
            |
| 461 | |||
| 462 | 16 |         if ($state !== self::STATE_MANAGED && $state !== self::STATE_REMOVED) { | 
            |
| 463 | 1 |             throw new \InvalidArgumentException("Entity has to be managed or scheduled for removal for single computation " . self::objToStr($entity)); | 
            |
| 464 | }  | 
            ||
| 465 | |||
| 466 | 15 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 467 | |||
| 468 | 15 |         if ($state === self::STATE_MANAGED && $class->isChangeTrackingDeferredImplicit()) { | 
            |
| 469 | 14 | $this->persist($entity);  | 
            |
| 470 | }  | 
            ||
| 471 | |||
| 472 | // Compute changes for INSERTed entities first. This must always happen even in this case.  | 
            ||
| 473 | 15 | $this->computeScheduleInsertsChangeSets();  | 
            |
| 474 | |||
| 475 | 15 |         if ($class->isReadOnly) { | 
            |
| 476 | return;  | 
            ||
| 477 | }  | 
            ||
| 478 | |||
| 479 | // Ignore uninitialized proxy objects  | 
            ||
| 480 | 15 |         if ($entity instanceof Proxy && ! $entity->__isInitialized__) { | 
            |
| 481 | 2 | return;  | 
            |
| 482 | }  | 
            ||
| 483 | |||
| 484 | // Only MANAGED entities that are NOT SCHEDULED FOR INSERTION OR DELETION are processed here.  | 
            ||
| 485 | 13 | $oid = spl_object_hash($entity);  | 
            |
| 486 | |||
| 487 | 13 | View Code Duplication |         if ( ! isset($this->entityInsertions[$oid]) && ! isset($this->entityDeletions[$oid]) && isset($this->entityStates[$oid])) { | 
            
| 488 | 6 | $this->computeChangeSet($class, $entity);  | 
            |
| 489 | }  | 
            ||
| 490 | 12 | }  | 
            |
| 491 | |||
| 492 | /**  | 
            ||
| 493 | * Executes any extra updates that have been scheduled.  | 
            ||
| 494 | */  | 
            ||
| 495 | 44 | private function executeExtraUpdates()  | 
            |
| 496 |     { | 
            ||
| 497 | 44 |         foreach ($this->extraUpdates as $oid => $update) { | 
            |
| 498 | 44 | list ($entity, $changeset) = $update;  | 
            |
| 499 | |||
| 500 | 44 | $this->entityChangeSets[$oid] = $changeset;  | 
            |
| 501 | 44 | $this->getEntityPersister(get_class($entity))->update($entity);  | 
            |
| 502 | }  | 
            ||
| 503 | |||
| 504 | 44 | $this->extraUpdates = [];  | 
            |
| 505 | 44 | }  | 
            |
| 506 | |||
| 507 | /**  | 
            ||
| 508 | * Gets the changeset for an entity.  | 
            ||
| 509 | *  | 
            ||
| 510 | * @param object $entity  | 
            ||
| 511 | *  | 
            ||
| 512 | * @return array  | 
            ||
| 513 | */  | 
            ||
| 514 | 1028 | public function & getEntityChangeSet($entity)  | 
            |
| 515 |     { | 
            ||
| 516 | 1028 | $oid = spl_object_hash($entity);  | 
            |
| 517 | 1028 | $data = [];  | 
            |
| 518 | |||
| 519 | 1028 |         if (!isset($this->entityChangeSets[$oid])) { | 
            |
| 520 | 1 | return $data;  | 
            |
| 521 | }  | 
            ||
| 522 | |||
| 523 | 1028 | return $this->entityChangeSets[$oid];  | 
            |
| 524 | }  | 
            ||
| 525 | |||
| 526 | /**  | 
            ||
| 527 | * Computes the changes that happened to a single entity.  | 
            ||
| 528 | *  | 
            ||
| 529 | * Modifies/populates the following properties:  | 
            ||
| 530 | *  | 
            ||
| 531 |      * {@link _originalEntityData} | 
            ||
| 532 | * If the entity is NEW or MANAGED but not yet fully persisted (only has an id)  | 
            ||
| 533 | * then it was not fetched from the database and therefore we have no original  | 
            ||
| 534 | * entity data yet. All of the current entity data is stored as the original entity data.  | 
            ||
| 535 | *  | 
            ||
| 536 |      * {@link _entityChangeSets} | 
            ||
| 537 | * The changes detected on all properties of the entity are stored there.  | 
            ||
| 538 | * A change is a tuple array where the first entry is the old value and the second  | 
            ||
| 539 | * entry is the new value of the property. Changesets are used by persisters  | 
            ||
| 540 | * to INSERT/UPDATE the persistent entity state.  | 
            ||
| 541 | *  | 
            ||
| 542 |      * {@link _entityUpdates} | 
            ||
| 543 | * If the entity is already fully MANAGED (has been fetched from the database before)  | 
            ||
| 544 | * and any changes to its properties are detected, then a reference to the entity is stored  | 
            ||
| 545 | * there to mark it for an update.  | 
            ||
| 546 | *  | 
            ||
| 547 |      * {@link _collectionDeletions} | 
            ||
| 548 | * If a PersistentCollection has been de-referenced in a fully MANAGED entity,  | 
            ||
| 549 | * then this collection is marked for deletion.  | 
            ||
| 550 | *  | 
            ||
| 551 | * @ignore  | 
            ||
| 552 | *  | 
            ||
| 553 | * @internal Don't call from the outside.  | 
            ||
| 554 | *  | 
            ||
| 555 | * @param ClassMetadata $class The class descriptor of the entity.  | 
            ||
| 556 | * @param object $entity The entity for which to compute the changes.  | 
            ||
| 557 | *  | 
            ||
| 558 | * @return void  | 
            ||
| 559 | */  | 
            ||
| 560 | 1038 | public function computeChangeSet(ClassMetadata $class, $entity)  | 
            |
| 561 |     { | 
            ||
| 562 | 1038 | $oid = spl_object_hash($entity);  | 
            |
| 563 | |||
| 564 | 1038 |         if (isset($this->readOnlyObjects[$oid])) { | 
            |
| 565 | 2 | return;  | 
            |
| 566 | }  | 
            ||
| 567 | |||
| 568 | 1038 |         if ( ! $class->isInheritanceTypeNone()) { | 
            |
| 569 | 316 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 570 | }  | 
            ||
| 571 | |||
| 572 | 1038 | $invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::preFlush) & ~ListenersInvoker::INVOKE_MANAGER;  | 
            |
| 573 | |||
| 574 | 1038 | View Code Duplication |         if ($invoke !== ListenersInvoker::INVOKE_NONE) { | 
            
| 575 | 138 | $this->listenersInvoker->invoke($class, Events::preFlush, $entity, new PreFlushEventArgs($this->em), $invoke);  | 
            |
| 576 | }  | 
            ||
| 577 | |||
| 578 | 1038 | $actualData = [];  | 
            |
| 579 | |||
| 580 | 1038 |         foreach ($class->reflFields as $name => $refProp) { | 
            |
| 581 | 1038 | $value = $refProp->getValue($entity);  | 
            |
| 582 | |||
| 583 | 1038 |             if ($class->isCollectionValuedAssociation($name) && $value !== null) { | 
            |
| 584 | 789 |                 if ($value instanceof PersistentCollection) { | 
            |
| 585 | 201 |                     if ($value->getOwner() === $entity) { | 
            |
| 586 | 201 | continue;  | 
            |
| 587 | }  | 
            ||
| 588 | |||
| 589 | 5 | $value = new ArrayCollection($value->getValues());  | 
            |
| 590 | }  | 
            ||
| 591 | |||
| 592 | // If $value is not a Collection then use an ArrayCollection.  | 
            ||
| 593 | 784 |                 if ( ! $value instanceof Collection) { | 
            |
| 594 | 242 | $value = new ArrayCollection($value);  | 
            |
| 595 | }  | 
            ||
| 596 | |||
| 597 | 784 | $assoc = $class->associationMappings[$name];  | 
            |
| 598 | |||
| 599 | // Inject PersistentCollection  | 
            ||
| 600 | 784 | $value = new PersistentCollection(  | 
            |
| 601 | 784 | $this->em, $this->em->getClassMetadata($assoc['targetEntity']), $value  | 
            |
| 602 | );  | 
            ||
| 603 | 784 | $value->setOwner($entity, $assoc);  | 
            |
| 604 | 784 | $value->setDirty( ! $value->isEmpty());  | 
            |
| 605 | |||
| 606 | 784 | $class->reflFields[$name]->setValue($entity, $value);  | 
            |
| 607 | |||
| 608 | 784 | $actualData[$name] = $value;  | 
            |
| 609 | |||
| 610 | 784 | continue;  | 
            |
| 611 | }  | 
            ||
| 612 | |||
| 613 | 1038 |             if (( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) && ($name !== $class->versionField)) { | 
            |
| 614 | 1038 | $actualData[$name] = $value;  | 
            |
| 615 | }  | 
            ||
| 616 | }  | 
            ||
| 617 | |||
| 618 | 1038 |         if ( ! isset($this->originalEntityData[$oid])) { | 
            |
| 619 | // Entity is either NEW or MANAGED but not yet fully persisted (only has an id).  | 
            ||
| 620 | // These result in an INSERT.  | 
            ||
| 621 | 1034 | $this->originalEntityData[$oid] = $actualData;  | 
            |
| 622 | 1034 | $changeSet = [];  | 
            |
| 623 | |||
| 624 | 1034 |             foreach ($actualData as $propName => $actualValue) { | 
            |
| 625 | 1017 | View Code Duplication |                 if ( ! isset($class->associationMappings[$propName])) { | 
            
| 626 | 965 | $changeSet[$propName] = [null, $actualValue];  | 
            |
| 627 | |||
| 628 | 965 | continue;  | 
            |
| 629 | }  | 
            ||
| 630 | |||
| 631 | 909 | $assoc = $class->associationMappings[$propName];  | 
            |
| 632 | |||
| 633 | 909 |                 if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) { | 
            |
| 634 | 909 | $changeSet[$propName] = [null, $actualValue];  | 
            |
| 635 | }  | 
            ||
| 636 | }  | 
            ||
| 637 | |||
| 638 | 1034 | $this->entityChangeSets[$oid] = $changeSet;  | 
            |
| 639 |         } else { | 
            ||
| 640 | // Entity is "fully" MANAGED: it was already fully persisted before  | 
            ||
| 641 | // and we have a copy of the original data  | 
            ||
| 642 | 268 | $originalData = $this->originalEntityData[$oid];  | 
            |
| 643 | 268 | $isChangeTrackingNotify = $class->isChangeTrackingNotify();  | 
            |
| 644 | 268 | $changeSet = ($isChangeTrackingNotify && isset($this->entityChangeSets[$oid]))  | 
            |
| 645 | ? $this->entityChangeSets[$oid]  | 
            ||
| 646 | 268 | : [];  | 
            |
| 647 | |||
| 648 | 268 |             foreach ($actualData as $propName => $actualValue) { | 
            |
| 649 | // skip field, its a partially omitted one!  | 
            ||
| 650 | 253 |                 if ( ! (isset($originalData[$propName]) || array_key_exists($propName, $originalData))) { | 
            |
| 651 | 8 | continue;  | 
            |
| 652 | }  | 
            ||
| 653 | |||
| 654 | 253 | $orgValue = $originalData[$propName];  | 
            |
| 655 | |||
| 656 | // skip if value haven't changed  | 
            ||
| 657 | 253 |                 if ($orgValue === $actualValue) { | 
            |
| 658 | 237 | continue;  | 
            |
| 659 | }  | 
            ||
| 660 | |||
| 661 | // if regular field  | 
            ||
| 662 | 114 | View Code Duplication |                 if ( ! isset($class->associationMappings[$propName])) { | 
            
| 663 | 60 |                     if ($isChangeTrackingNotify) { | 
            |
| 664 | continue;  | 
            ||
| 665 | }  | 
            ||
| 666 | |||
| 667 | 60 | $changeSet[$propName] = [$orgValue, $actualValue];  | 
            |
| 668 | |||
| 669 | 60 | continue;  | 
            |
| 670 | }  | 
            ||
| 671 | |||
| 672 | 58 | $assoc = $class->associationMappings[$propName];  | 
            |
| 673 | |||
| 674 | // Persistent collection was exchanged with the "originally"  | 
            ||
| 675 | // created one. This can only mean it was cloned and replaced  | 
            ||
| 676 | // on another entity.  | 
            ||
| 677 | 58 |                 if ($actualValue instanceof PersistentCollection) { | 
            |
| 678 | 8 | $owner = $actualValue->getOwner();  | 
            |
| 679 | 8 |                     if ($owner === null) { // cloned | 
            |
| 680 | $actualValue->setOwner($entity, $assoc);  | 
            ||
| 681 | 8 |                     } else if ($owner !== $entity) { // no clone, we have to fix | 
            |
| 682 |                         if (!$actualValue->isInitialized()) { | 
            ||
| 683 | $actualValue->initialize(); // we have to do this otherwise the cols share state  | 
            ||
| 684 | }  | 
            ||
| 685 | $newValue = clone $actualValue;  | 
            ||
| 686 | $newValue->setOwner($entity, $assoc);  | 
            ||
| 687 | $class->reflFields[$propName]->setValue($entity, $newValue);  | 
            ||
| 688 | }  | 
            ||
| 689 | }  | 
            ||
| 690 | |||
| 691 | 58 |                 if ($orgValue instanceof PersistentCollection) { | 
            |
| 692 | // A PersistentCollection was de-referenced, so delete it.  | 
            ||
| 693 | 8 | $coid = spl_object_hash($orgValue);  | 
            |
| 694 | |||
| 695 | 8 |                     if (isset($this->collectionDeletions[$coid])) { | 
            |
| 696 | continue;  | 
            ||
| 697 | }  | 
            ||
| 698 | |||
| 699 | 8 | $this->collectionDeletions[$coid] = $orgValue;  | 
            |
| 700 | 8 | $changeSet[$propName] = $orgValue; // Signal changeset, to-many assocs will be ignored.  | 
            |
| 701 | |||
| 702 | 8 | continue;  | 
            |
| 703 | }  | 
            ||
| 704 | |||
| 705 | 50 |                 if ($assoc['type'] & ClassMetadata::TO_ONE) { | 
            |
| 706 | 49 |                     if ($assoc['isOwningSide']) { | 
            |
| 707 | 21 | $changeSet[$propName] = [$orgValue, $actualValue];  | 
            |
| 708 | }  | 
            ||
| 709 | |||
| 710 | 49 |                     if ($orgValue !== null && $assoc['orphanRemoval']) { | 
            |
| 711 | 50 | $this->scheduleOrphanRemoval($orgValue);  | 
            |
| 712 | }  | 
            ||
| 713 | }  | 
            ||
| 714 | }  | 
            ||
| 715 | |||
| 716 | 268 |             if ($changeSet) { | 
            |
| 717 | 87 | $this->entityChangeSets[$oid] = $changeSet;  | 
            |
| 718 | 87 | $this->originalEntityData[$oid] = $actualData;  | 
            |
| 719 | 87 | $this->entityUpdates[$oid] = $entity;  | 
            |
| 720 | }  | 
            ||
| 721 | }  | 
            ||
| 722 | |||
| 723 | // Look for changes in associations of the entity  | 
            ||
| 724 | 1038 |         foreach ($class->associationMappings as $field => $assoc) { | 
            |
| 725 | 909 |             if (($val = $class->reflFields[$field]->getValue($entity)) === null) { | 
            |
| 726 | 642 | continue;  | 
            |
| 727 | }  | 
            ||
| 728 | |||
| 729 | 880 | $this->computeAssociationChanges($assoc, $val);  | 
            |
| 730 | |||
| 731 | 872 | if ( ! isset($this->entityChangeSets[$oid]) &&  | 
            |
| 732 | 872 | $assoc['isOwningSide'] &&  | 
            |
| 733 | 872 | $assoc['type'] == ClassMetadata::MANY_TO_MANY &&  | 
            |
| 734 | 872 | $val instanceof PersistentCollection &&  | 
            |
| 735 | 872 |                 $val->isDirty()) { | 
            |
| 736 | |||
| 737 | 35 | $this->entityChangeSets[$oid] = [];  | 
            |
| 738 | 35 | $this->originalEntityData[$oid] = $actualData;  | 
            |
| 739 | 872 | $this->entityUpdates[$oid] = $entity;  | 
            |
| 740 | }  | 
            ||
| 741 | }  | 
            ||
| 742 | 1030 | }  | 
            |
| 743 | |||
| 744 | /**  | 
            ||
| 745 | * Computes all the changes that have been done to entities and collections  | 
            ||
| 746 | * since the last commit and stores these changes in the _entityChangeSet map  | 
            ||
| 747 | * temporarily for access by the persisters, until the UoW commit is finished.  | 
            ||
| 748 | *  | 
            ||
| 749 | * @return void  | 
            ||
| 750 | */  | 
            ||
| 751 | 1029 | public function computeChangeSets()  | 
            |
| 752 |     { | 
            ||
| 753 | // Compute changes for INSERTed entities first. This must always happen.  | 
            ||
| 754 | 1029 | $this->computeScheduleInsertsChangeSets();  | 
            |
| 755 | |||
| 756 | // Compute changes for other MANAGED entities. Change tracking policies take effect here.  | 
            ||
| 757 | 1027 |         foreach ($this->identityMap as $className => $entities) { | 
            |
| 758 | 456 | $class = $this->em->getClassMetadata($className);  | 
            |
| 759 | |||
| 760 | // Skip class if instances are read-only  | 
            ||
| 761 | 456 |             if ($class->isReadOnly) { | 
            |
| 762 | 1 | continue;  | 
            |
| 763 | }  | 
            ||
| 764 | |||
| 765 | // If change tracking is explicit or happens through notification, then only compute  | 
            ||
| 766 | // changes on entities of that type that are explicitly marked for synchronization.  | 
            ||
| 767 |             switch (true) { | 
            ||
| 768 | 455 | case ($class->isChangeTrackingDeferredImplicit()):  | 
            |
| 769 | 453 | $entitiesToProcess = $entities;  | 
            |
| 770 | 453 | break;  | 
            |
| 771 | |||
| 772 | 3 | case (isset($this->scheduledForSynchronization[$className])):  | 
            |
| 773 | 3 | $entitiesToProcess = $this->scheduledForSynchronization[$className];  | 
            |
| 774 | 3 | break;  | 
            |
| 775 | |||
| 776 | default:  | 
            ||
| 777 | 1 | $entitiesToProcess = [];  | 
            |
| 778 | |||
| 779 | }  | 
            ||
| 780 | |||
| 781 | 455 |             foreach ($entitiesToProcess as $entity) { | 
            |
| 782 | // Ignore uninitialized proxy objects  | 
            ||
| 783 | 435 |                 if ($entity instanceof Proxy && ! $entity->__isInitialized__) { | 
            |
| 784 | 36 | continue;  | 
            |
| 785 | }  | 
            ||
| 786 | |||
| 787 | // Only MANAGED entities that are NOT SCHEDULED FOR INSERTION OR DELETION are processed here.  | 
            ||
| 788 | 434 | $oid = spl_object_hash($entity);  | 
            |
| 789 | |||
| 790 | 434 | View Code Duplication |                 if ( ! isset($this->entityInsertions[$oid]) && ! isset($this->entityDeletions[$oid]) && isset($this->entityStates[$oid])) { | 
            
| 791 | 455 | $this->computeChangeSet($class, $entity);  | 
            |
| 792 | }  | 
            ||
| 793 | }  | 
            ||
| 794 | }  | 
            ||
| 795 | 1027 | }  | 
            |
| 796 | |||
| 797 | /**  | 
            ||
| 798 | * Computes the changes of an association.  | 
            ||
| 799 | *  | 
            ||
| 800 | * @param array $assoc The association mapping.  | 
            ||
| 801 | * @param mixed $value The value of the association.  | 
            ||
| 802 | *  | 
            ||
| 803 | * @throws ORMInvalidArgumentException  | 
            ||
| 804 | * @throws ORMException  | 
            ||
| 805 | *  | 
            ||
| 806 | * @return void  | 
            ||
| 807 | */  | 
            ||
| 808 | 880 | private function computeAssociationChanges($assoc, $value)  | 
            |
| 809 |     { | 
            ||
| 810 | 880 |         if ($value instanceof Proxy && ! $value->__isInitialized__) { | 
            |
| 811 | 29 | return;  | 
            |
| 812 | }  | 
            ||
| 813 | |||
| 814 | 879 |         if ($value instanceof PersistentCollection && $value->isDirty()) { | 
            |
| 815 | 536 | $coid = spl_object_hash($value);  | 
            |
| 816 | |||
| 817 | 536 | $this->collectionUpdates[$coid] = $value;  | 
            |
| 818 | 536 | $this->visitedCollections[$coid] = $value;  | 
            |
| 819 | }  | 
            ||
| 820 | |||
| 821 | // Look through the entities, and in any of their associations,  | 
            ||
| 822 |         // for transient (new) entities, recursively. ("Persistence by reachability") | 
            ||
| 823 | // Unwrap. Uninitialized collections will simply be empty.  | 
            ||
| 824 | 879 | $unwrappedValue = ($assoc['type'] & ClassMetadata::TO_ONE) ? [$value] : $value->unwrap();  | 
            |
| 825 | 879 | $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);  | 
            |
| 826 | |||
| 827 | 879 |         foreach ($unwrappedValue as $key => $entry) { | 
            |
| 828 | 733 |             if (! ($entry instanceof $targetClass->name)) { | 
            |
| 829 | 6 | throw ORMInvalidArgumentException::invalidAssociation($targetClass, $assoc, $entry);  | 
            |
| 830 | }  | 
            ||
| 831 | |||
| 832 | 727 | $state = $this->getEntityState($entry, self::STATE_NEW);  | 
            |
| 833 | |||
| 834 | 727 |             if ( ! ($entry instanceof $assoc['targetEntity'])) { | 
            |
| 835 | throw ORMException::unexpectedAssociationValue($assoc['sourceEntity'], $assoc['fieldName'], get_class($entry), $assoc['targetEntity']);  | 
            ||
| 836 | }  | 
            ||
| 837 | |||
| 838 |             switch ($state) { | 
            ||
| 839 | 727 | case self::STATE_NEW:  | 
            |
| 840 | 40 |                     if ( ! $assoc['isCascadePersist']) { | 
            |
| 841 | 4 | throw ORMInvalidArgumentException::newEntityFoundThroughRelationship($assoc, $entry);  | 
            |
| 842 | }  | 
            ||
| 843 | |||
| 844 | 36 | $this->persistNew($targetClass, $entry);  | 
            |
| 845 | 36 | $this->computeChangeSet($targetClass, $entry);  | 
            |
| 846 | 36 | break;  | 
            |
| 847 | |||
| 848 | 721 | case self::STATE_REMOVED:  | 
            |
| 849 | // Consume the $value as array (it's either an array or an ArrayAccess)  | 
            ||
| 850 | // and remove the element from Collection.  | 
            ||
| 851 | 4 |                     if ($assoc['type'] & ClassMetadata::TO_MANY) { | 
            |
| 852 | 3 | unset($value[$key]);  | 
            |
| 853 | }  | 
            ||
| 854 | 4 | break;  | 
            |
| 855 | |||
| 856 | 721 | case self::STATE_DETACHED:  | 
            |
| 857 | // Can actually not happen right now as we assume STATE_NEW,  | 
            ||
| 858 | // so the exception will be raised from the DBAL layer (constraint violation).  | 
            ||
| 859 | throw ORMInvalidArgumentException::detachedEntityFoundThroughRelationship($assoc, $entry);  | 
            ||
| 860 | break;  | 
            ||
| 861 | |||
| 862 | 724 | default:  | 
            |
| 863 | // MANAGED associated entities are already taken into account  | 
            ||
| 864 | // during changeset calculation anyway, since they are in the identity map.  | 
            ||
| 865 | }  | 
            ||
| 866 | }  | 
            ||
| 867 | 871 | }  | 
            |
| 868 | |||
| 869 | /**  | 
            ||
| 870 | * @param \Doctrine\ORM\Mapping\ClassMetadata $class  | 
            ||
| 871 | * @param object $entity  | 
            ||
| 872 | *  | 
            ||
| 873 | * @return void  | 
            ||
| 874 | */  | 
            ||
| 875 | 1060 | private function persistNew($class, $entity)  | 
            |
| 876 |     { | 
            ||
| 877 | 1060 | $oid = spl_object_hash($entity);  | 
            |
| 878 | 1060 | $invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::prePersist);  | 
            |
| 879 | |||
| 880 | 1060 | View Code Duplication |         if ($invoke !== ListenersInvoker::INVOKE_NONE) { | 
            
| 881 | 141 | $this->listenersInvoker->invoke($class, Events::prePersist, $entity, new LifecycleEventArgs($entity, $this->em), $invoke);  | 
            |
| 882 | }  | 
            ||
| 883 | |||
| 884 | 1060 | $idGen = $class->idGenerator;  | 
            |
| 885 | |||
| 886 | 1060 |         if ( ! $idGen->isPostInsertGenerator()) { | 
            |
| 887 | 279 | $idValue = $idGen->generate($this->em, $entity);  | 
            |
| 888 | |||
| 889 | 279 |             if ( ! $idGen instanceof \Doctrine\ORM\Id\AssignedGenerator) { | 
            |
| 890 | 2 | $idValue = [$class->getSingleIdentifierFieldName() => $this->convertSingleFieldIdentifierToPHPValue($class, $idValue)];  | 
            |
| 891 | |||
| 892 | 2 | $class->setIdentifierValues($entity, $idValue);  | 
            |
| 893 | }  | 
            ||
| 894 | |||
| 895 | 279 | $this->entityIdentifiers[$oid] = $idValue;  | 
            |
| 896 | }  | 
            ||
| 897 | |||
| 898 | 1060 | $this->entityStates[$oid] = self::STATE_MANAGED;  | 
            |
| 899 | |||
| 900 | 1060 | $this->scheduleForInsert($entity);  | 
            |
| 901 | 1060 | }  | 
            |
| 902 | |||
| 903 | /**  | 
            ||
| 904 | * INTERNAL:  | 
            ||
| 905 | * Computes the changeset of an individual entity, independently of the  | 
            ||
| 906 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit().  | 
            ||
| 907 | *  | 
            ||
| 908 | * The passed entity must be a managed entity. If the entity already has a change set  | 
            ||
| 909 | * because this method is invoked during a commit cycle then the change sets are added.  | 
            ||
| 910 | * whereby changes detected in this method prevail.  | 
            ||
| 911 | *  | 
            ||
| 912 | * @ignore  | 
            ||
| 913 | *  | 
            ||
| 914 | * @param ClassMetadata $class The class descriptor of the entity.  | 
            ||
| 915 | * @param object $entity The entity for which to (re)calculate the change set.  | 
            ||
| 916 | *  | 
            ||
| 917 | * @return void  | 
            ||
| 918 | *  | 
            ||
| 919 | * @throws ORMInvalidArgumentException If the passed entity is not MANAGED.  | 
            ||
| 920 | */  | 
            ||
| 921 | 16 | public function recomputeSingleEntityChangeSet(ClassMetadata $class, $entity)  | 
            |
| 922 |     { | 
            ||
| 923 | 16 | $oid = spl_object_hash($entity);  | 
            |
| 924 | |||
| 925 | 16 |         if ( ! isset($this->entityStates[$oid]) || $this->entityStates[$oid] != self::STATE_MANAGED) { | 
            |
| 926 | throw ORMInvalidArgumentException::entityNotManaged($entity);  | 
            ||
| 927 | }  | 
            ||
| 928 | |||
| 929 | // skip if change tracking is "NOTIFY"  | 
            ||
| 930 | 16 |         if ($class->isChangeTrackingNotify()) { | 
            |
| 931 | return;  | 
            ||
| 932 | }  | 
            ||
| 933 | |||
| 934 | 16 |         if ( ! $class->isInheritanceTypeNone()) { | 
            |
| 935 | 3 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 936 | }  | 
            ||
| 937 | |||
| 938 | 16 | $actualData = [];  | 
            |
| 939 | |||
| 940 | 16 |         foreach ($class->reflFields as $name => $refProp) { | 
            |
| 941 | 16 | if (( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity())  | 
            |
| 942 | 16 | && ($name !== $class->versionField)  | 
            |
| 943 | 16 |                 && ! $class->isCollectionValuedAssociation($name)) { | 
            |
| 944 | 16 | $actualData[$name] = $refProp->getValue($entity);  | 
            |
| 945 | }  | 
            ||
| 946 | }  | 
            ||
| 947 | |||
| 948 | 16 |         if ( ! isset($this->originalEntityData[$oid])) { | 
            |
| 949 |             throw new \RuntimeException('Cannot call recomputeSingleEntityChangeSet before computeChangeSet on an entity.'); | 
            ||
| 950 | }  | 
            ||
| 951 | |||
| 952 | 16 | $originalData = $this->originalEntityData[$oid];  | 
            |
| 953 | 16 | $changeSet = [];  | 
            |
| 954 | |||
| 955 | 16 |         foreach ($actualData as $propName => $actualValue) { | 
            |
| 956 | 16 | $orgValue = isset($originalData[$propName]) ? $originalData[$propName] : null;  | 
            |
| 957 | |||
| 958 | 16 |             if ($orgValue !== $actualValue) { | 
            |
| 959 | 16 | $changeSet[$propName] = [$orgValue, $actualValue];  | 
            |
| 960 | }  | 
            ||
| 961 | }  | 
            ||
| 962 | |||
| 963 | 16 |         if ($changeSet) { | 
            |
| 964 | 7 |             if (isset($this->entityChangeSets[$oid])) { | 
            |
| 965 | 6 | $this->entityChangeSets[$oid] = array_merge($this->entityChangeSets[$oid], $changeSet);  | 
            |
| 966 | 1 |             } else if ( ! isset($this->entityInsertions[$oid])) { | 
            |
| 967 | 1 | $this->entityChangeSets[$oid] = $changeSet;  | 
            |
| 968 | 1 | $this->entityUpdates[$oid] = $entity;  | 
            |
| 969 | }  | 
            ||
| 970 | 7 | $this->originalEntityData[$oid] = $actualData;  | 
            |
| 971 | }  | 
            ||
| 972 | 16 | }  | 
            |
| 973 | |||
| 974 | /**  | 
            ||
| 975 | * Executes all entity insertions for entities of the specified type.  | 
            ||
| 976 | *  | 
            ||
| 977 | * @param \Doctrine\ORM\Mapping\ClassMetadata $class  | 
            ||
| 978 | *  | 
            ||
| 979 | * @return void  | 
            ||
| 980 | */  | 
            ||
| 981 | 1026 | private function executeInserts($class)  | 
            |
| 982 |     { | 
            ||
| 983 | 1026 | $entities = [];  | 
            |
| 984 | 1026 | $className = $class->name;  | 
            |
| 985 | 1026 | $persister = $this->getEntityPersister($className);  | 
            |
| 986 | 1026 | $invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::postPersist);  | 
            |
| 987 | |||
| 988 | 1026 |         foreach ($this->entityInsertions as $oid => $entity) { | 
            |
| 989 | |||
| 990 | 1026 |             if ($this->em->getClassMetadata(get_class($entity))->name !== $className) { | 
            |
| 991 | 872 | continue;  | 
            |
| 992 | }  | 
            ||
| 993 | |||
| 994 | 1026 | $persister->addInsert($entity);  | 
            |
| 995 | |||
| 996 | 1026 | unset($this->entityInsertions[$oid]);  | 
            |
| 997 | |||
| 998 | 1026 |             if ($invoke !== ListenersInvoker::INVOKE_NONE) { | 
            |
| 999 | 1026 | $entities[] = $entity;  | 
            |
| 1000 | }  | 
            ||
| 1001 | }  | 
            ||
| 1002 | |||
| 1003 | 1026 | $postInsertIds = $persister->executeInserts();  | 
            |
| 1004 | |||
| 1005 | 1026 |         if ($postInsertIds) { | 
            |
| 1006 | // Persister returned post-insert IDs  | 
            ||
| 1007 | 935 |             foreach ($postInsertIds as $postInsertId) { | 
            |
| 1008 | 935 | $idField = $class->getSingleIdentifierFieldName();  | 
            |
| 1009 | 935 | $idValue = $this->convertSingleFieldIdentifierToPHPValue($class, $postInsertId['generatedId']);  | 
            |
| 1010 | |||
| 1011 | 935 | $entity = $postInsertId['entity'];  | 
            |
| 1012 | 935 | $oid = spl_object_hash($entity);  | 
            |
| 1013 | |||
| 1014 | 935 | $class->reflFields[$idField]->setValue($entity, $idValue);  | 
            |
| 1015 | |||
| 1016 | 935 | $this->entityIdentifiers[$oid] = [$idField => $idValue];  | 
            |
| 1017 | 935 | $this->entityStates[$oid] = self::STATE_MANAGED;  | 
            |
| 1018 | 935 | $this->originalEntityData[$oid][$idField] = $idValue;  | 
            |
| 1019 | |||
| 1020 | 935 | $this->addToIdentityMap($entity);  | 
            |
| 1021 | }  | 
            ||
| 1022 | }  | 
            ||
| 1023 | |||
| 1024 | 1026 |         foreach ($entities as $entity) { | 
            |
| 1025 | 136 | $this->listenersInvoker->invoke($class, Events::postPersist, $entity, new LifecycleEventArgs($entity, $this->em), $invoke);  | 
            |
| 1026 | }  | 
            ||
| 1027 | 1026 | }  | 
            |
| 1028 | |||
| 1029 | /**  | 
            ||
| 1030 | * Executes all entity updates for entities of the specified type.  | 
            ||
| 1031 | *  | 
            ||
| 1032 | * @param \Doctrine\ORM\Mapping\ClassMetadata $class  | 
            ||
| 1033 | *  | 
            ||
| 1034 | * @return void  | 
            ||
| 1035 | */  | 
            ||
| 1036 | 118 | private function executeUpdates($class)  | 
            |
| 1037 |     { | 
            ||
| 1038 | 118 | $className = $class->name;  | 
            |
| 1039 | 118 | $persister = $this->getEntityPersister($className);  | 
            |
| 1040 | 118 | $preUpdateInvoke = $this->listenersInvoker->getSubscribedSystems($class, Events::preUpdate);  | 
            |
| 1041 | 118 | $postUpdateInvoke = $this->listenersInvoker->getSubscribedSystems($class, Events::postUpdate);  | 
            |
| 1042 | |||
| 1043 | 118 |         foreach ($this->entityUpdates as $oid => $entity) { | 
            |
| 1044 | 118 |             if ($this->em->getClassMetadata(get_class($entity))->name !== $className) { | 
            |
| 1045 | 76 | continue;  | 
            |
| 1046 | }  | 
            ||
| 1047 | |||
| 1048 | 118 |             if ($preUpdateInvoke != ListenersInvoker::INVOKE_NONE) { | 
            |
| 1049 | 13 | $this->listenersInvoker->invoke($class, Events::preUpdate, $entity, new PreUpdateEventArgs($entity, $this->em, $this->getEntityChangeSet($entity)), $preUpdateInvoke);  | 
            |
| 1050 | |||
| 1051 | 13 | $this->recomputeSingleEntityChangeSet($class, $entity);  | 
            |
| 1052 | }  | 
            ||
| 1053 | |||
| 1054 | 118 |             if ( ! empty($this->entityChangeSets[$oid])) { | 
            |
| 1055 | 84 | $persister->update($entity);  | 
            |
| 1056 | }  | 
            ||
| 1057 | |||
| 1058 | 114 | unset($this->entityUpdates[$oid]);  | 
            |
| 1059 | |||
| 1060 | 114 | View Code Duplication |             if ($postUpdateInvoke != ListenersInvoker::INVOKE_NONE) { | 
            
| 1061 | 114 | $this->listenersInvoker->invoke($class, Events::postUpdate, $entity, new LifecycleEventArgs($entity, $this->em), $postUpdateInvoke);  | 
            |
| 1062 | }  | 
            ||
| 1063 | }  | 
            ||
| 1064 | 114 | }  | 
            |
| 1065 | |||
| 1066 | /**  | 
            ||
| 1067 | * Executes all entity deletions for entities of the specified type.  | 
            ||
| 1068 | *  | 
            ||
| 1069 | * @param \Doctrine\ORM\Mapping\ClassMetadata $class  | 
            ||
| 1070 | *  | 
            ||
| 1071 | * @return void  | 
            ||
| 1072 | */  | 
            ||
| 1073 | 63 | private function executeDeletions($class)  | 
            |
| 1074 |     { | 
            ||
| 1075 | 63 | $className = $class->name;  | 
            |
| 1076 | 63 | $persister = $this->getEntityPersister($className);  | 
            |
| 1077 | 63 | $invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::postRemove);  | 
            |
| 1078 | |||
| 1079 | 63 |         foreach ($this->entityDeletions as $oid => $entity) { | 
            |
| 1080 | 63 |             if ($this->em->getClassMetadata(get_class($entity))->name !== $className) { | 
            |
| 1081 | 25 | continue;  | 
            |
| 1082 | }  | 
            ||
| 1083 | |||
| 1084 | 63 | $persister->delete($entity);  | 
            |
| 1085 | |||
| 1086 | unset(  | 
            ||
| 1087 | 63 | $this->entityDeletions[$oid],  | 
            |
| 1088 | 63 | $this->entityIdentifiers[$oid],  | 
            |
| 1089 | 63 | $this->originalEntityData[$oid],  | 
            |
| 1090 | 63 | $this->entityStates[$oid]  | 
            |
| 1091 | );  | 
            ||
| 1092 | |||
| 1093 | // Entity with this $oid after deletion treated as NEW, even if the $oid  | 
            ||
| 1094 | // is obtained by a new entity because the old one went out of scope.  | 
            ||
| 1095 | //$this->entityStates[$oid] = self::STATE_NEW;  | 
            ||
| 1096 | 63 |             if ( ! $class->isIdentifierNatural()) { | 
            |
| 1097 | 53 | $class->reflFields[$class->identifier[0]]->setValue($entity, null);  | 
            |
| 1098 | }  | 
            ||
| 1099 | |||
| 1100 | 63 | View Code Duplication |             if ($invoke !== ListenersInvoker::INVOKE_NONE) { | 
            
| 1101 | 63 | $this->listenersInvoker->invoke($class, Events::postRemove, $entity, new LifecycleEventArgs($entity, $this->em), $invoke);  | 
            |
| 1102 | }  | 
            ||
| 1103 | }  | 
            ||
| 1104 | 62 | }  | 
            |
| 1105 | |||
| 1106 | /**  | 
            ||
| 1107 | * Gets the commit order.  | 
            ||
| 1108 | *  | 
            ||
| 1109 | * @param array|null $entityChangeSet  | 
            ||
| 1110 | *  | 
            ||
| 1111 | * @return array  | 
            ||
| 1112 | */  | 
            ||
| 1113 | 1030 | private function getCommitOrder(array $entityChangeSet = null)  | 
            |
| 1114 |     { | 
            ||
| 1115 | 1030 |         if ($entityChangeSet === null) { | 
            |
| 1116 | 1030 | $entityChangeSet = array_merge($this->entityInsertions, $this->entityUpdates, $this->entityDeletions);  | 
            |
| 1117 | }  | 
            ||
| 1118 | |||
| 1119 | 1030 | $calc = $this->getCommitOrderCalculator();  | 
            |
| 1120 | |||
| 1121 | // See if there are any new classes in the changeset, that are not in the  | 
            ||
| 1122 | // commit order graph yet (don't have a node).  | 
            ||
| 1123 | // We have to inspect changeSet to be able to correctly build dependencies.  | 
            ||
| 1124 | // It is not possible to use IdentityMap here because post inserted ids  | 
            ||
| 1125 | // are not yet available.  | 
            ||
| 1126 | 1030 | $newNodes = [];  | 
            |
| 1127 | |||
| 1128 | 1030 |         foreach ($entityChangeSet as $entity) { | 
            |
| 1129 | 1030 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 1130 | |||
| 1131 | 1030 |             if ($calc->hasNode($class->name)) { | 
            |
| 1132 | 633 | continue;  | 
            |
| 1133 | }  | 
            ||
| 1134 | |||
| 1135 | 1030 | $calc->addNode($class->name, $class);  | 
            |
| 1136 | |||
| 1137 | 1030 | $newNodes[] = $class;  | 
            |
| 1138 | }  | 
            ||
| 1139 | |||
| 1140 | // Calculate dependencies for new nodes  | 
            ||
| 1141 | 1030 |         while ($class = array_pop($newNodes)) { | 
            |
| 1142 | 1030 |             foreach ($class->associationMappings as $assoc) { | 
            |
| 1143 | 901 |                 if ( ! ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE)) { | 
            |
| 1144 | 860 | continue;  | 
            |
| 1145 | }  | 
            ||
| 1146 | |||
| 1147 | 854 | $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);  | 
            |
| 1148 | |||
| 1149 | 854 |                 if ( ! $calc->hasNode($targetClass->name)) { | 
            |
| 1150 | 659 | $calc->addNode($targetClass->name, $targetClass);  | 
            |
| 1151 | |||
| 1152 | 659 | $newNodes[] = $targetClass;  | 
            |
| 1153 | }  | 
            ||
| 1154 | |||
| 1155 | 854 | $joinColumns = reset($assoc['joinColumns']);  | 
            |
| 1156 | |||
| 1157 | 854 | $calc->addDependency($targetClass->name, $class->name, (int)empty($joinColumns['nullable']));  | 
            |
| 1158 | |||
| 1159 | // If the target class has mapped subclasses, these share the same dependency.  | 
            ||
| 1160 | 854 |                 if ( ! $targetClass->subClasses) { | 
            |
| 1161 | 847 | continue;  | 
            |
| 1162 | }  | 
            ||
| 1163 | |||
| 1164 | 226 |                 foreach ($targetClass->subClasses as $subClassName) { | 
            |
| 1165 | 226 | $targetSubClass = $this->em->getClassMetadata($subClassName);  | 
            |
| 1166 | |||
| 1167 | 226 |                     if ( ! $calc->hasNode($subClassName)) { | 
            |
| 1168 | 196 | $calc->addNode($targetSubClass->name, $targetSubClass);  | 
            |
| 1169 | |||
| 1170 | 196 | $newNodes[] = $targetSubClass;  | 
            |
| 1171 | }  | 
            ||
| 1172 | |||
| 1173 | 226 | $calc->addDependency($targetSubClass->name, $class->name, 1);  | 
            |
| 1174 | }  | 
            ||
| 1175 | }  | 
            ||
| 1176 | }  | 
            ||
| 1177 | |||
| 1178 | 1030 | return $calc->sort();  | 
            |
| 1179 | }  | 
            ||
| 1180 | |||
| 1181 | /**  | 
            ||
| 1182 | * Schedules an entity for insertion into the database.  | 
            ||
| 1183 | * If the entity already has an identifier, it will be added to the identity map.  | 
            ||
| 1184 | *  | 
            ||
| 1185 | * @param object $entity The entity to schedule for insertion.  | 
            ||
| 1186 | *  | 
            ||
| 1187 | * @return void  | 
            ||
| 1188 | *  | 
            ||
| 1189 | * @throws ORMInvalidArgumentException  | 
            ||
| 1190 | * @throws \InvalidArgumentException  | 
            ||
| 1191 | */  | 
            ||
| 1192 | 1061 | public function scheduleForInsert($entity)  | 
            |
| 1193 |     { | 
            ||
| 1194 | 1061 | $oid = spl_object_hash($entity);  | 
            |
| 1195 | |||
| 1196 | 1061 |         if (isset($this->entityUpdates[$oid])) { | 
            |
| 1197 |             throw new InvalidArgumentException("Dirty entity can not be scheduled for insertion."); | 
            ||
| 1198 | }  | 
            ||
| 1199 | |||
| 1200 | 1061 |         if (isset($this->entityDeletions[$oid])) { | 
            |
| 1201 | 1 | throw ORMInvalidArgumentException::scheduleInsertForRemovedEntity($entity);  | 
            |
| 1202 | }  | 
            ||
| 1203 | 1061 |         if (isset($this->originalEntityData[$oid]) && ! isset($this->entityInsertions[$oid])) { | 
            |
| 1204 | 1 | throw ORMInvalidArgumentException::scheduleInsertForManagedEntity($entity);  | 
            |
| 1205 | }  | 
            ||
| 1206 | |||
| 1207 | 1061 |         if (isset($this->entityInsertions[$oid])) { | 
            |
| 1208 | 1 | throw ORMInvalidArgumentException::scheduleInsertTwice($entity);  | 
            |
| 1209 | }  | 
            ||
| 1210 | |||
| 1211 | 1061 | $this->entityInsertions[$oid] = $entity;  | 
            |
| 1212 | |||
| 1213 | 1061 |         if (isset($this->entityIdentifiers[$oid])) { | 
            |
| 1214 | 279 | $this->addToIdentityMap($entity);  | 
            |
| 1215 | }  | 
            ||
| 1216 | |||
| 1217 | 1061 |         if ($entity instanceof NotifyPropertyChanged) { | 
            |
| 1218 | 5 | $entity->addPropertyChangedListener($this);  | 
            |
| 1219 | }  | 
            ||
| 1220 | 1061 | }  | 
            |
| 1221 | |||
| 1222 | /**  | 
            ||
| 1223 | * Checks whether an entity is scheduled for insertion.  | 
            ||
| 1224 | *  | 
            ||
| 1225 | * @param object $entity  | 
            ||
| 1226 | *  | 
            ||
| 1227 | * @return boolean  | 
            ||
| 1228 | */  | 
            ||
| 1229 | 644 | public function isScheduledForInsert($entity)  | 
            |
| 1230 |     { | 
            ||
| 1231 | 644 | return isset($this->entityInsertions[spl_object_hash($entity)]);  | 
            |
| 1232 | }  | 
            ||
| 1233 | |||
| 1234 | /**  | 
            ||
| 1235 | * Schedules an entity for being updated.  | 
            ||
| 1236 | *  | 
            ||
| 1237 | * @param object $entity The entity to schedule for being updated.  | 
            ||
| 1238 | *  | 
            ||
| 1239 | * @return void  | 
            ||
| 1240 | *  | 
            ||
| 1241 | * @throws ORMInvalidArgumentException  | 
            ||
| 1242 | */  | 
            ||
| 1243 | 1 | public function scheduleForUpdate($entity)  | 
            |
| 1244 |     { | 
            ||
| 1245 | 1 | $oid = spl_object_hash($entity);  | 
            |
| 1246 | |||
| 1247 | 1 |         if ( ! isset($this->entityIdentifiers[$oid])) { | 
            |
| 1248 | throw ORMInvalidArgumentException::entityHasNoIdentity($entity, "scheduling for update");  | 
            ||
| 1249 | }  | 
            ||
| 1250 | |||
| 1251 | 1 |         if (isset($this->entityDeletions[$oid])) { | 
            |
| 1252 | throw ORMInvalidArgumentException::entityIsRemoved($entity, "schedule for update");  | 
            ||
| 1253 | }  | 
            ||
| 1254 | |||
| 1255 | 1 |         if ( ! isset($this->entityUpdates[$oid]) && ! isset($this->entityInsertions[$oid])) { | 
            |
| 1256 | 1 | $this->entityUpdates[$oid] = $entity;  | 
            |
| 1257 | }  | 
            ||
| 1258 | 1 | }  | 
            |
| 1259 | |||
| 1260 | /**  | 
            ||
| 1261 | * INTERNAL:  | 
            ||
| 1262 | * Schedules an extra update that will be executed immediately after the  | 
            ||
| 1263 | * regular entity updates within the currently running commit cycle.  | 
            ||
| 1264 | *  | 
            ||
| 1265 | * Extra updates for entities are stored as (entity, changeset) tuples.  | 
            ||
| 1266 | *  | 
            ||
| 1267 | * @ignore  | 
            ||
| 1268 | *  | 
            ||
| 1269 | * @param object $entity The entity for which to schedule an extra update.  | 
            ||
| 1270 | * @param array $changeset The changeset of the entity (what to update).  | 
            ||
| 1271 | *  | 
            ||
| 1272 | * @return void  | 
            ||
| 1273 | */  | 
            ||
| 1274 | 44 | public function scheduleExtraUpdate($entity, array $changeset)  | 
            |
| 1275 |     { | 
            ||
| 1276 | 44 | $oid = spl_object_hash($entity);  | 
            |
| 1277 | 44 | $extraUpdate = [$entity, $changeset];  | 
            |
| 1278 | |||
| 1279 | 44 |         if (isset($this->extraUpdates[$oid])) { | 
            |
| 1280 | 1 | list(, $changeset2) = $this->extraUpdates[$oid];  | 
            |
| 1281 | |||
| 1282 | 1 | $extraUpdate = [$entity, $changeset + $changeset2];  | 
            |
| 1283 | }  | 
            ||
| 1284 | |||
| 1285 | 44 | $this->extraUpdates[$oid] = $extraUpdate;  | 
            |
| 1286 | 44 | }  | 
            |
| 1287 | |||
| 1288 | /**  | 
            ||
| 1289 | * Checks whether an entity is registered as dirty in the unit of work.  | 
            ||
| 1290 | * Note: Is not very useful currently as dirty entities are only registered  | 
            ||
| 1291 | * at commit time.  | 
            ||
| 1292 | *  | 
            ||
| 1293 | * @param object $entity  | 
            ||
| 1294 | *  | 
            ||
| 1295 | * @return boolean  | 
            ||
| 1296 | */  | 
            ||
| 1297 | public function isScheduledForUpdate($entity)  | 
            ||
| 1298 |     { | 
            ||
| 1299 | return isset($this->entityUpdates[spl_object_hash($entity)]);  | 
            ||
| 1300 | }  | 
            ||
| 1301 | |||
| 1302 | /**  | 
            ||
| 1303 | * Checks whether an entity is registered to be checked in the unit of work.  | 
            ||
| 1304 | *  | 
            ||
| 1305 | * @param object $entity  | 
            ||
| 1306 | *  | 
            ||
| 1307 | * @return boolean  | 
            ||
| 1308 | */  | 
            ||
| 1309 | 1 | public function isScheduledForDirtyCheck($entity)  | 
            |
| 1310 |     { | 
            ||
| 1311 | 1 | $rootEntityName = $this->em->getClassMetadata(get_class($entity))->rootEntityName;  | 
            |
| 1312 | |||
| 1313 | 1 | return isset($this->scheduledForSynchronization[$rootEntityName][spl_object_hash($entity)]);  | 
            |
| 1314 | }  | 
            ||
| 1315 | |||
| 1316 | /**  | 
            ||
| 1317 | * INTERNAL:  | 
            ||
| 1318 | * Schedules an entity for deletion.  | 
            ||
| 1319 | *  | 
            ||
| 1320 | * @param object $entity  | 
            ||
| 1321 | *  | 
            ||
| 1322 | * @return void  | 
            ||
| 1323 | */  | 
            ||
| 1324 | 66 | public function scheduleForDelete($entity)  | 
            |
| 1325 |     { | 
            ||
| 1326 | 66 | $oid = spl_object_hash($entity);  | 
            |
| 1327 | |||
| 1328 | 66 |         if (isset($this->entityInsertions[$oid])) { | 
            |
| 1329 | 1 |             if ($this->isInIdentityMap($entity)) { | 
            |
| 1330 | $this->removeFromIdentityMap($entity);  | 
            ||
| 1331 | }  | 
            ||
| 1332 | |||
| 1333 | 1 | unset($this->entityInsertions[$oid], $this->entityStates[$oid]);  | 
            |
| 1334 | |||
| 1335 | 1 | return; // entity has not been persisted yet, so nothing more to do.  | 
            |
| 1336 | }  | 
            ||
| 1337 | |||
| 1338 | 66 |         if ( ! $this->isInIdentityMap($entity)) { | 
            |
| 1339 | 1 | return;  | 
            |
| 1340 | }  | 
            ||
| 1341 | |||
| 1342 | 65 | $this->removeFromIdentityMap($entity);  | 
            |
| 1343 | |||
| 1344 | 65 | unset($this->entityUpdates[$oid]);  | 
            |
| 1345 | |||
| 1346 | 65 |         if ( ! isset($this->entityDeletions[$oid])) { | 
            |
| 1347 | 65 | $this->entityDeletions[$oid] = $entity;  | 
            |
| 1348 | 65 | $this->entityStates[$oid] = self::STATE_REMOVED;  | 
            |
| 1349 | }  | 
            ||
| 1350 | 65 | }  | 
            |
| 1351 | |||
| 1352 | /**  | 
            ||
| 1353 | * Checks whether an entity is registered as removed/deleted with the unit  | 
            ||
| 1354 | * of work.  | 
            ||
| 1355 | *  | 
            ||
| 1356 | * @param object $entity  | 
            ||
| 1357 | *  | 
            ||
| 1358 | * @return boolean  | 
            ||
| 1359 | */  | 
            ||
| 1360 | 17 | public function isScheduledForDelete($entity)  | 
            |
| 1361 |     { | 
            ||
| 1362 | 17 | return isset($this->entityDeletions[spl_object_hash($entity)]);  | 
            |
| 1363 | }  | 
            ||
| 1364 | |||
| 1365 | /**  | 
            ||
| 1366 | * Checks whether an entity is scheduled for insertion, update or deletion.  | 
            ||
| 1367 | *  | 
            ||
| 1368 | * @param object $entity  | 
            ||
| 1369 | *  | 
            ||
| 1370 | * @return boolean  | 
            ||
| 1371 | */  | 
            ||
| 1372 | public function isEntityScheduled($entity)  | 
            ||
| 1373 |     { | 
            ||
| 1374 | $oid = spl_object_hash($entity);  | 
            ||
| 1375 | |||
| 1376 | return isset($this->entityInsertions[$oid])  | 
            ||
| 1377 | || isset($this->entityUpdates[$oid])  | 
            ||
| 1378 | || isset($this->entityDeletions[$oid]);  | 
            ||
| 1379 | }  | 
            ||
| 1380 | |||
| 1381 | /**  | 
            ||
| 1382 | * INTERNAL:  | 
            ||
| 1383 | * Registers an entity in the identity map.  | 
            ||
| 1384 | * Note that entities in a hierarchy are registered with the class name of  | 
            ||
| 1385 | * the root entity.  | 
            ||
| 1386 | *  | 
            ||
| 1387 | * @ignore  | 
            ||
| 1388 | *  | 
            ||
| 1389 | * @param object $entity The entity to register.  | 
            ||
| 1390 | *  | 
            ||
| 1391 | * @return boolean TRUE if the registration was successful, FALSE if the identity of  | 
            ||
| 1392 | * the entity in question is already managed.  | 
            ||
| 1393 | *  | 
            ||
| 1394 | * @throws ORMInvalidArgumentException  | 
            ||
| 1395 | */  | 
            ||
| 1396 | 1127 | public function addToIdentityMap($entity)  | 
            |
| 1397 |     { | 
            ||
| 1398 | 1127 | $classMetadata = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 1399 | 1127 | $identifier = $this->entityIdentifiers[spl_object_hash($entity)];  | 
            |
| 1400 | |||
| 1401 | 1127 |         if (empty($identifier) || in_array(null, $identifier, true)) { | 
            |
| 1402 | 6 | throw ORMInvalidArgumentException::entityWithoutIdentity($classMetadata->name, $entity);  | 
            |
| 1403 | }  | 
            ||
| 1404 | |||
| 1405 | 1121 | $idHash = $this->hashIdentifier($identifier);  | 
            |
| 1406 | 1121 | $className = $classMetadata->rootEntityName;  | 
            |
| 1407 | |||
| 1408 | 1121 |         if (isset($this->identityMap[$className][$idHash])) { | 
            |
| 1409 | 85 | return false;  | 
            |
| 1410 | }  | 
            ||
| 1411 | |||
| 1412 | 1121 | $this->identityMap[$className][$idHash] = $entity;  | 
            |
| 1413 | |||
| 1414 | 1121 | return true;  | 
            |
| 1415 | }  | 
            ||
| 1416 | |||
| 1417 | /**  | 
            ||
| 1418 | * Gets the state of an entity with regard to the current unit of work.  | 
            ||
| 1419 | *  | 
            ||
| 1420 | * @param object $entity  | 
            ||
| 1421 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED).  | 
            ||
| 1422 | * This parameter can be set to improve performance of entity state detection  | 
            ||
| 1423 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED  | 
            ||
| 1424 | * is either known or does not matter for the caller of the method.  | 
            ||
| 1425 | *  | 
            ||
| 1426 | * @return int The entity state.  | 
            ||
| 1427 | */  | 
            ||
| 1428 | 1075 | public function getEntityState($entity, $assume = null)  | 
            |
| 1429 |     { | 
            ||
| 1430 | 1075 | $oid = spl_object_hash($entity);  | 
            |
| 1431 | |||
| 1432 | 1075 |         if (isset($this->entityStates[$oid])) { | 
            |
| 1433 | 797 | return $this->entityStates[$oid];  | 
            |
| 1434 | }  | 
            ||
| 1435 | |||
| 1436 | 1069 |         if ($assume !== null) { | 
            |
| 1437 | 1065 | return $assume;  | 
            |
| 1438 | }  | 
            ||
| 1439 | |||
| 1440 | // State can only be NEW or DETACHED, because MANAGED/REMOVED states are known.  | 
            ||
| 1441 | // Note that you can not remember the NEW or DETACHED state in _entityStates since  | 
            ||
| 1442 | // the UoW does not hold references to such objects and the object hash can be reused.  | 
            ||
| 1443 | // More generally because the state may "change" between NEW/DETACHED without the UoW being aware of it.  | 
            ||
| 1444 | 13 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 1445 | 13 | $id = $class->getIdentifierValues($entity);  | 
            |
| 1446 | |||
| 1447 | 13 |         if ( ! $id) { | 
            |
| 1448 | 5 | return self::STATE_NEW;  | 
            |
| 1449 | }  | 
            ||
| 1450 | |||
| 1451 | 10 |         if ($class->containsForeignIdentifier) { | 
            |
| 1452 | 1 | $id = $this->identifierFlattener->flattenIdentifier($class, $id);  | 
            |
| 1453 | }  | 
            ||
| 1454 | |||
| 1455 |         switch (true) { | 
            ||
| 1456 | 10 | case ($class->isIdentifierNatural()):  | 
            |
| 1457 | // Check for a version field, if available, to avoid a db lookup.  | 
            ||
| 1458 | 5 |                 if ($class->isVersioned) { | 
            |
| 1459 | 1 | return ($class->getFieldValue($entity, $class->versionField))  | 
            |
| 1460 | ? self::STATE_DETACHED  | 
            ||
| 1461 | 1 | : self::STATE_NEW;  | 
            |
| 1462 | }  | 
            ||
| 1463 | |||
| 1464 | // Last try before db lookup: check the identity map.  | 
            ||
| 1465 | 4 |                 if ($this->tryGetById($id, $class->rootEntityName)) { | 
            |
| 1466 | 1 | return self::STATE_DETACHED;  | 
            |
| 1467 | }  | 
            ||
| 1468 | |||
| 1469 | // db lookup  | 
            ||
| 1470 | 4 |                 if ($this->getEntityPersister($class->name)->exists($entity)) { | 
            |
| 1471 | return self::STATE_DETACHED;  | 
            ||
| 1472 | }  | 
            ||
| 1473 | |||
| 1474 | 4 | return self::STATE_NEW;  | 
            |
| 1475 | |||
| 1476 | 5 | case ( ! $class->idGenerator->isPostInsertGenerator()):  | 
            |
| 1477 | // if we have a pre insert generator we can't be sure that having an id  | 
            ||
| 1478 | // really means that the entity exists. We have to verify this through  | 
            ||
| 1479 | // the last resort: a db lookup  | 
            ||
| 1480 | |||
| 1481 | // Last try before db lookup: check the identity map.  | 
            ||
| 1482 |                 if ($this->tryGetById($id, $class->rootEntityName)) { | 
            ||
| 1483 | return self::STATE_DETACHED;  | 
            ||
| 1484 | }  | 
            ||
| 1485 | |||
| 1486 | // db lookup  | 
            ||
| 1487 |                 if ($this->getEntityPersister($class->name)->exists($entity)) { | 
            ||
| 1488 | return self::STATE_DETACHED;  | 
            ||
| 1489 | }  | 
            ||
| 1490 | |||
| 1491 | return self::STATE_NEW;  | 
            ||
| 1492 | |||
| 1493 | default:  | 
            ||
| 1494 | 5 | return self::STATE_DETACHED;  | 
            |
| 1495 | }  | 
            ||
| 1496 | }  | 
            ||
| 1497 | |||
| 1498 | /**  | 
            ||
| 1499 | * INTERNAL:  | 
            ||
| 1500 | * Removes an entity from the identity map. This effectively detaches the  | 
            ||
| 1501 | * entity from the persistence management of Doctrine.  | 
            ||
| 1502 | *  | 
            ||
| 1503 | * @ignore  | 
            ||
| 1504 | *  | 
            ||
| 1505 | * @param object $entity  | 
            ||
| 1506 | *  | 
            ||
| 1507 | * @return boolean  | 
            ||
| 1508 | *  | 
            ||
| 1509 | * @throws ORMInvalidArgumentException  | 
            ||
| 1510 | */  | 
            ||
| 1511 | 78 | public function removeFromIdentityMap($entity)  | 
            |
| 1512 |     { | 
            ||
| 1513 | 78 | $oid = spl_object_hash($entity);  | 
            |
| 1514 | 78 | $classMetadata = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 1515 | 78 | $idHash = $this->hashIdentifier($this->entityIdentifiers[$oid]);  | 
            |
| 1516 | |||
| 1517 | 78 |         if ($idHash === '') { | 
            |
| 1518 | throw ORMInvalidArgumentException::entityHasNoIdentity($entity, "remove from identity map");  | 
            ||
| 1519 | }  | 
            ||
| 1520 | |||
| 1521 | 78 | $className = $classMetadata->rootEntityName;  | 
            |
| 1522 | |||
| 1523 | 78 |         if (isset($this->identityMap[$className][$idHash])) { | 
            |
| 1524 | 78 | unset($this->identityMap[$className][$idHash]);  | 
            |
| 1525 | 78 | unset($this->readOnlyObjects[$oid]);  | 
            |
| 1526 | |||
| 1527 | //$this->entityStates[$oid] = self::STATE_DETACHED;  | 
            ||
| 1528 | |||
| 1529 | 78 | return true;  | 
            |
| 1530 | }  | 
            ||
| 1531 | |||
| 1532 | return false;  | 
            ||
| 1533 | }  | 
            ||
| 1534 | |||
| 1535 | /**  | 
            ||
| 1536 | * INTERNAL:  | 
            ||
| 1537 | * Gets an entity in the identity map by its identifier hash.  | 
            ||
| 1538 | *  | 
            ||
| 1539 | * @ignore  | 
            ||
| 1540 | *  | 
            ||
| 1541 | * @param string $idHash  | 
            ||
| 1542 | * @param string $rootClassName  | 
            ||
| 1543 | *  | 
            ||
| 1544 | * @return object  | 
            ||
| 1545 | */  | 
            ||
| 1546 | 6 | public function getByIdHash($idHash, $rootClassName)  | 
            |
| 1547 |     { | 
            ||
| 1548 | 6 | return $this->identityMap[$rootClassName][$idHash];  | 
            |
| 1549 | }  | 
            ||
| 1550 | |||
| 1551 | /**  | 
            ||
| 1552 | * INTERNAL:  | 
            ||
| 1553 | * Tries to get an entity by its identifier hash. If no entity is found for  | 
            ||
| 1554 | * the given hash, FALSE is returned.  | 
            ||
| 1555 | *  | 
            ||
| 1556 | * @ignore  | 
            ||
| 1557 | *  | 
            ||
| 1558 | * @param mixed $idHash (must be possible to cast it to string)  | 
            ||
| 1559 | * @param string $rootClassName  | 
            ||
| 1560 | *  | 
            ||
| 1561 | * @return object|bool The found entity or FALSE.  | 
            ||
| 1562 | */  | 
            ||
| 1563 | 34 | View Code Duplication | public function tryGetByIdHash($idHash, $rootClassName)  | 
            
| 1564 |     { | 
            ||
| 1565 | 34 | $stringIdHash = (string) $idHash;  | 
            |
| 1566 | |||
| 1567 | 34 | return isset($this->identityMap[$rootClassName][$stringIdHash])  | 
            |
| 1568 | ? $this->identityMap[$rootClassName][$stringIdHash]  | 
            ||
| 1569 | 34 | : false;  | 
            |
| 1570 | }  | 
            ||
| 1571 | |||
| 1572 | /**  | 
            ||
| 1573 | * Checks whether an entity is registered in the identity map of this UnitOfWork.  | 
            ||
| 1574 | *  | 
            ||
| 1575 | * @param object $entity  | 
            ||
| 1576 | *  | 
            ||
| 1577 | * @return boolean  | 
            ||
| 1578 | */  | 
            ||
| 1579 | 216 | public function isInIdentityMap($entity)  | 
            |
| 1580 |     { | 
            ||
| 1581 | 216 | $oid = spl_object_hash($entity);  | 
            |
| 1582 | |||
| 1583 | 216 |         if (empty($this->entityIdentifiers[$oid])) { | 
            |
| 1584 | 33 | return false;  | 
            |
| 1585 | }  | 
            ||
| 1586 | |||
| 1587 | 200 | $classMetadata = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 1588 | 200 | $idHash = $this->hashIdentifier($this->entityIdentifiers[$oid]);  | 
            |
| 1589 | |||
| 1590 | 200 | return isset($this->identityMap[$classMetadata->rootEntityName][$idHash]);  | 
            |
| 1591 | }  | 
            ||
| 1592 | |||
| 1593 | /**  | 
            ||
| 1594 | * INTERNAL:  | 
            ||
| 1595 | * Checks whether an identifier hash exists in the identity map.  | 
            ||
| 1596 | *  | 
            ||
| 1597 | * @ignore  | 
            ||
| 1598 | *  | 
            ||
| 1599 | * @param string $idHash  | 
            ||
| 1600 | * @param string $rootClassName  | 
            ||
| 1601 | *  | 
            ||
| 1602 | * @return boolean  | 
            ||
| 1603 | */  | 
            ||
| 1604 | public function containsIdHash($idHash, $rootClassName)  | 
            ||
| 1605 |     { | 
            ||
| 1606 | return isset($this->identityMap[$rootClassName][$idHash]);  | 
            ||
| 1607 | }  | 
            ||
| 1608 | |||
| 1609 | /**  | 
            ||
| 1610 | * Persists an entity as part of the current unit of work.  | 
            ||
| 1611 | *  | 
            ||
| 1612 | * @param object $entity The entity to persist.  | 
            ||
| 1613 | *  | 
            ||
| 1614 | * @return void  | 
            ||
| 1615 | */  | 
            ||
| 1616 | 1056 | public function persist($entity)  | 
            |
| 1617 |     { | 
            ||
| 1618 | 1056 | $visited = [];  | 
            |
| 1619 | |||
| 1620 | 1056 | $this->doPersist($entity, $visited);  | 
            |
| 1621 | 1049 | }  | 
            |
| 1622 | |||
| 1623 | /**  | 
            ||
| 1624 | * Persists an entity as part of the current unit of work.  | 
            ||
| 1625 | *  | 
            ||
| 1626 | * This method is internally called during persist() cascades as it tracks  | 
            ||
| 1627 | * the already visited entities to prevent infinite recursions.  | 
            ||
| 1628 | *  | 
            ||
| 1629 | * @param object $entity The entity to persist.  | 
            ||
| 1630 | * @param array $visited The already visited entities.  | 
            ||
| 1631 | *  | 
            ||
| 1632 | * @return void  | 
            ||
| 1633 | *  | 
            ||
| 1634 | * @throws ORMInvalidArgumentException  | 
            ||
| 1635 | * @throws UnexpectedValueException  | 
            ||
| 1636 | */  | 
            ||
| 1637 | 1056 | private function doPersist($entity, array &$visited)  | 
            |
| 1638 |     { | 
            ||
| 1639 | 1056 | $oid = spl_object_hash($entity);  | 
            |
| 1640 | |||
| 1641 | 1056 |         if (isset($visited[$oid])) { | 
            |
| 1642 | 109 | return; // Prevent infinite recursion  | 
            |
| 1643 | }  | 
            ||
| 1644 | |||
| 1645 | 1056 | $visited[$oid] = $entity; // Mark visited  | 
            |
| 1646 | |||
| 1647 | 1056 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 1648 | |||
| 1649 | // We assume NEW, so DETACHED entities result in an exception on flush (constraint violation).  | 
            ||
| 1650 | // If we would detect DETACHED here we would throw an exception anyway with the same  | 
            ||
| 1651 | // consequences (not recoverable/programming error), so just assuming NEW here  | 
            ||
| 1652 | // lets us avoid some database lookups for entities with natural identifiers.  | 
            ||
| 1653 | 1056 | $entityState = $this->getEntityState($entity, self::STATE_NEW);  | 
            |
| 1654 | |||
| 1655 |         switch ($entityState) { | 
            ||
| 1656 | 1056 | case self::STATE_MANAGED:  | 
            |
| 1657 | // Nothing to do, except if policy is "deferred explicit"  | 
            ||
| 1658 | 236 |                 if ($class->isChangeTrackingDeferredExplicit()) { | 
            |
| 1659 | 2 | $this->scheduleForDirtyCheck($entity);  | 
            |
| 1660 | }  | 
            ||
| 1661 | 236 | break;  | 
            |
| 1662 | |||
| 1663 | 1056 | case self::STATE_NEW:  | 
            |
| 1664 | 1055 | $this->persistNew($class, $entity);  | 
            |
| 1665 | 1055 | break;  | 
            |
| 1666 | |||
| 1667 | 1 | case self::STATE_REMOVED:  | 
            |
| 1668 | // Entity becomes managed again  | 
            ||
| 1669 | 1 | unset($this->entityDeletions[$oid]);  | 
            |
| 1670 | 1 | $this->addToIdentityMap($entity);  | 
            |
| 1671 | |||
| 1672 | 1 | $this->entityStates[$oid] = self::STATE_MANAGED;  | 
            |
| 1673 | 1 | break;  | 
            |
| 1674 | |||
| 1675 | case self::STATE_DETACHED:  | 
            ||
| 1676 | // Can actually not happen right now since we assume STATE_NEW.  | 
            ||
| 1677 | throw ORMInvalidArgumentException::detachedEntityCannot($entity, "persisted");  | 
            ||
| 1678 | |||
| 1679 | default:  | 
            ||
| 1680 |                 throw new UnexpectedValueException("Unexpected entity state: $entityState." . self::objToStr($entity)); | 
            ||
| 1681 | }  | 
            ||
| 1682 | |||
| 1683 | 1056 | $this->cascadePersist($entity, $visited);  | 
            |
| 1684 | 1049 | }  | 
            |
| 1685 | |||
| 1686 | /**  | 
            ||
| 1687 | * Deletes an entity as part of the current unit of work.  | 
            ||
| 1688 | *  | 
            ||
| 1689 | * @param object $entity The entity to remove.  | 
            ||
| 1690 | *  | 
            ||
| 1691 | * @return void  | 
            ||
| 1692 | */  | 
            ||
| 1693 | 65 | public function remove($entity)  | 
            |
| 1694 |     { | 
            ||
| 1695 | 65 | $visited = [];  | 
            |
| 1696 | |||
| 1697 | 65 | $this->doRemove($entity, $visited);  | 
            |
| 1698 | 65 | }  | 
            |
| 1699 | |||
| 1700 | /**  | 
            ||
| 1701 | * Deletes an entity as part of the current unit of work.  | 
            ||
| 1702 | *  | 
            ||
| 1703 | * This method is internally called during delete() cascades as it tracks  | 
            ||
| 1704 | * the already visited entities to prevent infinite recursions.  | 
            ||
| 1705 | *  | 
            ||
| 1706 | * @param object $entity The entity to delete.  | 
            ||
| 1707 | * @param array $visited The map of the already visited entities.  | 
            ||
| 1708 | *  | 
            ||
| 1709 | * @return void  | 
            ||
| 1710 | *  | 
            ||
| 1711 | * @throws ORMInvalidArgumentException If the instance is a detached entity.  | 
            ||
| 1712 | * @throws UnexpectedValueException  | 
            ||
| 1713 | */  | 
            ||
| 1714 | 65 | private function doRemove($entity, array &$visited)  | 
            |
| 1715 |     { | 
            ||
| 1716 | 65 | $oid = spl_object_hash($entity);  | 
            |
| 1717 | |||
| 1718 | 65 |         if (isset($visited[$oid])) { | 
            |
| 1719 | 1 | return; // Prevent infinite recursion  | 
            |
| 1720 | }  | 
            ||
| 1721 | |||
| 1722 | 65 | $visited[$oid] = $entity; // mark visited  | 
            |
| 1723 | |||
| 1724 | // Cascade first, because scheduleForDelete() removes the entity from the identity map, which  | 
            ||
| 1725 | // can cause problems when a lazy proxy has to be initialized for the cascade operation.  | 
            ||
| 1726 | 65 | $this->cascadeRemove($entity, $visited);  | 
            |
| 1727 | |||
| 1728 | 65 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 1729 | 65 | $entityState = $this->getEntityState($entity);  | 
            |
| 1730 | |||
| 1731 |         switch ($entityState) { | 
            ||
| 1732 | 65 | case self::STATE_NEW:  | 
            |
| 1733 | 65 | case self::STATE_REMOVED:  | 
            |
| 1734 | // nothing to do  | 
            ||
| 1735 | 2 | break;  | 
            |
| 1736 | |||
| 1737 | 65 | case self::STATE_MANAGED:  | 
            |
| 1738 | 65 | $invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::preRemove);  | 
            |
| 1739 | |||
| 1740 | 65 | View Code Duplication |                 if ($invoke !== ListenersInvoker::INVOKE_NONE) { | 
            
| 1741 | 8 | $this->listenersInvoker->invoke($class, Events::preRemove, $entity, new LifecycleEventArgs($entity, $this->em), $invoke);  | 
            |
| 1742 | }  | 
            ||
| 1743 | |||
| 1744 | 65 | $this->scheduleForDelete($entity);  | 
            |
| 1745 | 65 | break;  | 
            |
| 1746 | |||
| 1747 | case self::STATE_DETACHED:  | 
            ||
| 1748 | throw ORMInvalidArgumentException::detachedEntityCannot($entity, "removed");  | 
            ||
| 1749 | default:  | 
            ||
| 1750 |                 throw new UnexpectedValueException("Unexpected entity state: $entityState." . self::objToStr($entity)); | 
            ||
| 1751 | }  | 
            ||
| 1752 | |||
| 1753 | 65 | }  | 
            |
| 1754 | |||
| 1755 | /**  | 
            ||
| 1756 | * Merges the state of the given detached entity into this UnitOfWork.  | 
            ||
| 1757 | *  | 
            ||
| 1758 | * @param object $entity  | 
            ||
| 1759 | *  | 
            ||
| 1760 | * @return object The managed copy of the entity.  | 
            ||
| 1761 | *  | 
            ||
| 1762 | * @throws OptimisticLockException If the entity uses optimistic locking through a version  | 
            ||
| 1763 | * attribute and the version check against the managed copy fails.  | 
            ||
| 1764 | *  | 
            ||
| 1765 | * @todo Require active transaction!? OptimisticLockException may result in undefined state!?  | 
            ||
| 1766 | */  | 
            ||
| 1767 | 43 | public function merge($entity)  | 
            |
| 1768 |     { | 
            ||
| 1769 | 43 | $visited = [];  | 
            |
| 1770 | |||
| 1771 | 43 | return $this->doMerge($entity, $visited);  | 
            |
| 1772 | }  | 
            ||
| 1773 | |||
| 1774 | /**  | 
            ||
| 1775 | * Executes a merge operation on an entity.  | 
            ||
| 1776 | *  | 
            ||
| 1777 | * @param object $entity  | 
            ||
| 1778 | * @param array $visited  | 
            ||
| 1779 | * @param object|null $prevManagedCopy  | 
            ||
| 1780 | * @param array|null $assoc  | 
            ||
| 1781 | *  | 
            ||
| 1782 | * @return object The managed copy of the entity.  | 
            ||
| 1783 | *  | 
            ||
| 1784 | * @throws OptimisticLockException If the entity uses optimistic locking through a version  | 
            ||
| 1785 | * attribute and the version check against the managed copy fails.  | 
            ||
| 1786 | * @throws ORMInvalidArgumentException If the entity instance is NEW.  | 
            ||
| 1787 | * @throws EntityNotFoundException if an assigned identifier is used in the entity, but none is provided  | 
            ||
| 1788 | */  | 
            ||
| 1789 | 43 | private function doMerge($entity, array &$visited, $prevManagedCopy = null, array $assoc = [])  | 
            |
| 1790 |     { | 
            ||
| 1791 | 43 | $oid = spl_object_hash($entity);  | 
            |
| 1792 | |||
| 1793 | 43 |         if (isset($visited[$oid])) { | 
            |
| 1794 | 4 | $managedCopy = $visited[$oid];  | 
            |
| 1795 | |||
| 1796 | 4 |             if ($prevManagedCopy !== null) { | 
            |
| 1797 | 4 | $this->updateAssociationWithMergedEntity($entity, $assoc, $prevManagedCopy, $managedCopy);  | 
            |
| 1798 | }  | 
            ||
| 1799 | |||
| 1800 | 4 | return $managedCopy;  | 
            |
| 1801 | }  | 
            ||
| 1802 | |||
| 1803 | 43 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 1804 | |||
| 1805 | // First we assume DETACHED, although it can still be NEW but we can avoid  | 
            ||
| 1806 | // an extra db-roundtrip this way. If it is not MANAGED but has an identity,  | 
            ||
| 1807 | // we need to fetch it from the db anyway in order to merge.  | 
            ||
| 1808 | // MANAGED entities are ignored by the merge operation.  | 
            ||
| 1809 | 43 | $managedCopy = $entity;  | 
            |
| 1810 | |||
| 1811 | 43 |         if ($this->getEntityState($entity, self::STATE_DETACHED) !== self::STATE_MANAGED) { | 
            |
| 1812 | // Try to look the entity up in the identity map.  | 
            ||
| 1813 | 42 | $id = $class->getIdentifierValues($entity);  | 
            |
| 1814 | |||
| 1815 | // If there is no ID, it is actually NEW.  | 
            ||
| 1816 | 42 |             if ( ! $id) { | 
            |
| 1817 | 6 | $managedCopy = $this->newInstance($class);  | 
            |
| 1818 | |||
| 1819 | 6 | $this->mergeEntityStateIntoManagedCopy($entity, $managedCopy);  | 
            |
| 1820 | 6 | $this->persistNew($class, $managedCopy);  | 
            |
| 1821 |             } else { | 
            ||
| 1822 | 37 | $flatId = ($class->containsForeignIdentifier)  | 
            |
| 1823 | 3 | ? $this->identifierFlattener->flattenIdentifier($class, $id)  | 
            |
| 1824 | 37 | : $id;  | 
            |
| 1825 | |||
| 1826 | 37 | $managedCopy = $this->tryGetById($flatId, $class->rootEntityName);  | 
            |
| 1827 | |||
| 1828 | 37 |                 if ($managedCopy) { | 
            |
| 1829 | // We have the entity in-memory already, just make sure its not removed.  | 
            ||
| 1830 | 15 |                     if ($this->getEntityState($managedCopy) == self::STATE_REMOVED) { | 
            |
| 1831 | 15 | throw ORMInvalidArgumentException::entityIsRemoved($managedCopy, "merge");  | 
            |
| 1832 | }  | 
            ||
| 1833 |                 } else { | 
            ||
| 1834 | // We need to fetch the managed copy in order to merge.  | 
            ||
| 1835 | 25 | $managedCopy = $this->em->find($class->name, $flatId);  | 
            |
| 1836 | }  | 
            ||
| 1837 | |||
| 1838 | 37 |                 if ($managedCopy === null) { | 
            |
| 1839 | // If the identifier is ASSIGNED, it is NEW, otherwise an error  | 
            ||
| 1840 | // since the managed entity was not found.  | 
            ||
| 1841 | 3 |                     if ( ! $class->isIdentifierNatural()) { | 
            |
| 1842 | 1 | throw EntityNotFoundException::fromClassNameAndIdentifier(  | 
            |
| 1843 | 1 | $class->getName(),  | 
            |
| 1844 | 1 | $this->identifierFlattener->flattenIdentifier($class, $id)  | 
            |
| 1845 | );  | 
            ||
| 1846 | }  | 
            ||
| 1847 | |||
| 1848 | 2 | $managedCopy = $this->newInstance($class);  | 
            |
| 1849 | 2 | $class->setIdentifierValues($managedCopy, $id);  | 
            |
| 1850 | |||
| 1851 | 2 | $this->mergeEntityStateIntoManagedCopy($entity, $managedCopy);  | 
            |
| 1852 | 2 | $this->persistNew($class, $managedCopy);  | 
            |
| 1853 |                 } else { | 
            ||
| 1854 | 34 | $this->ensureVersionMatch($class, $entity, $managedCopy);  | 
            |
| 1855 | 33 | $this->mergeEntityStateIntoManagedCopy($entity, $managedCopy);  | 
            |
| 1856 | }  | 
            ||
| 1857 | }  | 
            ||
| 1858 | |||
| 1859 | 40 | $visited[$oid] = $managedCopy; // mark visited  | 
            |
| 1860 | |||
| 1861 | 40 |             if ($class->isChangeTrackingDeferredExplicit()) { | 
            |
| 1862 | $this->scheduleForDirtyCheck($entity);  | 
            ||
| 1863 | }  | 
            ||
| 1864 | }  | 
            ||
| 1865 | |||
| 1866 | 41 |         if ($prevManagedCopy !== null) { | 
            |
| 1867 | 6 | $this->updateAssociationWithMergedEntity($entity, $assoc, $prevManagedCopy, $managedCopy);  | 
            |
| 1868 | }  | 
            ||
| 1869 | |||
| 1870 | // Mark the managed copy visited as well  | 
            ||
| 1871 | 41 | $visited[spl_object_hash($managedCopy)] = $managedCopy;  | 
            |
| 1872 | |||
| 1873 | 41 | $this->cascadeMerge($entity, $managedCopy, $visited);  | 
            |
| 1874 | |||
| 1875 | 41 | return $managedCopy;  | 
            |
| 1876 | }  | 
            ||
| 1877 | |||
| 1878 | /**  | 
            ||
| 1879 | * @param ClassMetadata $class  | 
            ||
| 1880 | * @param object $entity  | 
            ||
| 1881 | * @param object $managedCopy  | 
            ||
| 1882 | *  | 
            ||
| 1883 | * @return void  | 
            ||
| 1884 | *  | 
            ||
| 1885 | * @throws OptimisticLockException  | 
            ||
| 1886 | */  | 
            ||
| 1887 | 34 | private function ensureVersionMatch(ClassMetadata $class, $entity, $managedCopy)  | 
            |
| 1888 |     { | 
            ||
| 1889 | 34 |         if (! ($class->isVersioned && $this->isLoaded($managedCopy) && $this->isLoaded($entity))) { | 
            |
| 1890 | 31 | return;  | 
            |
| 1891 | }  | 
            ||
| 1892 | |||
| 1893 | 4 | $reflField = $class->reflFields[$class->versionField];  | 
            |
| 1894 | 4 | $managedCopyVersion = $reflField->getValue($managedCopy);  | 
            |
| 1895 | 4 | $entityVersion = $reflField->getValue($entity);  | 
            |
| 1896 | |||
| 1897 | // Throw exception if versions don't match.  | 
            ||
| 1898 | 4 |         if ($managedCopyVersion == $entityVersion) { | 
            |
| 1899 | 3 | return;  | 
            |
| 1900 | }  | 
            ||
| 1901 | |||
| 1902 | 1 | throw OptimisticLockException::lockFailedVersionMismatch($entity, $entityVersion, $managedCopyVersion);  | 
            |
| 1903 | }  | 
            ||
| 1904 | |||
| 1905 | /**  | 
            ||
| 1906 | * Tests if an entity is loaded - must either be a loaded proxy or not a proxy  | 
            ||
| 1907 | *  | 
            ||
| 1908 | * @param object $entity  | 
            ||
| 1909 | *  | 
            ||
| 1910 | * @return bool  | 
            ||
| 1911 | */  | 
            ||
| 1912 | 41 | private function isLoaded($entity)  | 
            |
| 1916 | |||
| 1917 | /**  | 
            ||
| 1918 | * Sets/adds associated managed copies into the previous entity's association field  | 
            ||
| 1919 | *  | 
            ||
| 1920 | * @param object $entity  | 
            ||
| 1921 | * @param array $association  | 
            ||
| 1922 | * @param object $previousManagedCopy  | 
            ||
| 1923 | * @param object $managedCopy  | 
            ||
| 1924 | *  | 
            ||
| 1925 | * @return void  | 
            ||
| 1926 | */  | 
            ||
| 1927 | 6 | private function updateAssociationWithMergedEntity($entity, array $association, $previousManagedCopy, $managedCopy)  | 
            |
| 1928 |     { | 
            ||
| 1929 | 6 | $assocField = $association['fieldName'];  | 
            |
| 1930 | 6 | $prevClass = $this->em->getClassMetadata(get_class($previousManagedCopy));  | 
            |
| 1931 | |||
| 1932 | 6 |         if ($association['type'] & ClassMetadata::TO_ONE) { | 
            |
| 1933 | 6 | $prevClass->reflFields[$assocField]->setValue($previousManagedCopy, $managedCopy);  | 
            |
| 1934 | |||
| 1935 | 6 | return;  | 
            |
| 1936 | }  | 
            ||
| 1937 | |||
| 1938 | 1 | $value = $prevClass->reflFields[$assocField]->getValue($previousManagedCopy);  | 
            |
| 1939 | 1 | $value[] = $managedCopy;  | 
            |
| 1940 | |||
| 1941 | 1 | View Code Duplication |         if ($association['type'] == ClassMetadata::ONE_TO_MANY) { | 
            
| 1942 | 1 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 1943 | |||
| 1944 | 1 | $class->reflFields[$association['mappedBy']]->setValue($managedCopy, $previousManagedCopy);  | 
            |
| 1945 | }  | 
            ||
| 1946 | 1 | }  | 
            |
| 1947 | |||
| 1948 | /**  | 
            ||
| 1949 | * Detaches an entity from the persistence management. It's persistence will  | 
            ||
| 1950 | * no longer be managed by Doctrine.  | 
            ||
| 1951 | *  | 
            ||
| 1952 | * @param object $entity The entity to detach.  | 
            ||
| 1953 | *  | 
            ||
| 1954 | * @return void  | 
            ||
| 1955 | */  | 
            ||
| 1956 | 12 | public function detach($entity)  | 
            |
| 1957 |     { | 
            ||
| 1958 | 12 | $visited = [];  | 
            |
| 1959 | |||
| 1960 | 12 | $this->doDetach($entity, $visited);  | 
            |
| 1961 | 12 | }  | 
            |
| 1962 | |||
| 1963 | /**  | 
            ||
| 1964 | * Executes a detach operation on the given entity.  | 
            ||
| 1965 | *  | 
            ||
| 1966 | * @param object $entity  | 
            ||
| 1967 | * @param array $visited  | 
            ||
| 1968 | * @param boolean $noCascade if true, don't cascade detach operation.  | 
            ||
| 1969 | *  | 
            ||
| 1970 | * @return void  | 
            ||
| 1971 | */  | 
            ||
| 1972 | 16 | private function doDetach($entity, array &$visited, $noCascade = false)  | 
            |
| 1973 |     { | 
            ||
| 1974 | 16 | $oid = spl_object_hash($entity);  | 
            |
| 1975 | |||
| 1976 | 16 |         if (isset($visited[$oid])) { | 
            |
| 1977 | return; // Prevent infinite recursion  | 
            ||
| 1978 | }  | 
            ||
| 1979 | |||
| 1980 | 16 | $visited[$oid] = $entity; // mark visited  | 
            |
| 1981 | |||
| 1982 | 16 |         switch ($this->getEntityState($entity, self::STATE_DETACHED)) { | 
            |
| 1983 | 16 | case self::STATE_MANAGED:  | 
            |
| 1984 | 14 |                 if ($this->isInIdentityMap($entity)) { | 
            |
| 1985 | 13 | $this->removeFromIdentityMap($entity);  | 
            |
| 1986 | }  | 
            ||
| 1987 | |||
| 1988 | unset(  | 
            ||
| 1989 | 14 | $this->entityInsertions[$oid],  | 
            |
| 1990 | 14 | $this->entityUpdates[$oid],  | 
            |
| 1991 | 14 | $this->entityDeletions[$oid],  | 
            |
| 1992 | 14 | $this->entityIdentifiers[$oid],  | 
            |
| 1993 | 14 | $this->entityStates[$oid],  | 
            |
| 1994 | 14 | $this->originalEntityData[$oid]  | 
            |
| 1995 | );  | 
            ||
| 1996 | 14 | break;  | 
            |
| 1997 | 3 | case self::STATE_NEW:  | 
            |
| 1998 | 3 | case self::STATE_DETACHED:  | 
            |
| 1999 | 3 | return;  | 
            |
| 2000 | }  | 
            ||
| 2001 | |||
| 2002 | 14 |         if ( ! $noCascade) { | 
            |
| 2003 | 14 | $this->cascadeDetach($entity, $visited);  | 
            |
| 2004 | }  | 
            ||
| 2005 | 14 | }  | 
            |
| 2006 | |||
| 2007 | /**  | 
            ||
| 2008 | * Refreshes the state of the given entity from the database, overwriting  | 
            ||
| 2009 | * any local, unpersisted changes.  | 
            ||
| 2010 | *  | 
            ||
| 2011 | * @param object $entity The entity to refresh.  | 
            ||
| 2012 | *  | 
            ||
| 2013 | * @return void  | 
            ||
| 2014 | *  | 
            ||
| 2015 | * @throws InvalidArgumentException If the entity is not MANAGED.  | 
            ||
| 2016 | */  | 
            ||
| 2017 | 17 | public function refresh($entity)  | 
            |
| 2018 |     { | 
            ||
| 2019 | 17 | $visited = [];  | 
            |
| 2020 | |||
| 2021 | 17 | $this->doRefresh($entity, $visited);  | 
            |
| 2022 | 17 | }  | 
            |
| 2023 | |||
| 2024 | /**  | 
            ||
| 2025 | * Executes a refresh operation on an entity.  | 
            ||
| 2026 | *  | 
            ||
| 2027 | * @param object $entity The entity to refresh.  | 
            ||
| 2028 | * @param array $visited The already visited entities during cascades.  | 
            ||
| 2029 | *  | 
            ||
| 2030 | * @return void  | 
            ||
| 2031 | *  | 
            ||
| 2032 | * @throws ORMInvalidArgumentException If the entity is not MANAGED.  | 
            ||
| 2033 | */  | 
            ||
| 2034 | 17 | private function doRefresh($entity, array &$visited)  | 
            |
| 2035 |     { | 
            ||
| 2036 | 17 | $oid = spl_object_hash($entity);  | 
            |
| 2037 | |||
| 2038 | 17 |         if (isset($visited[$oid])) { | 
            |
| 2039 | return; // Prevent infinite recursion  | 
            ||
| 2040 | }  | 
            ||
| 2041 | |||
| 2042 | 17 | $visited[$oid] = $entity; // mark visited  | 
            |
| 2043 | |||
| 2044 | 17 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 2045 | |||
| 2046 | 17 |         if ($this->getEntityState($entity) !== self::STATE_MANAGED) { | 
            |
| 2047 | throw ORMInvalidArgumentException::entityNotManaged($entity);  | 
            ||
| 2048 | }  | 
            ||
| 2049 | |||
| 2050 | 17 | $this->getEntityPersister($class->name)->refresh(  | 
            |
| 2051 | 17 | array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]),  | 
            |
| 2052 | 17 | $entity  | 
            |
| 2053 | );  | 
            ||
| 2054 | |||
| 2055 | 17 | $this->cascadeRefresh($entity, $visited);  | 
            |
| 2056 | 17 | }  | 
            |
| 2057 | |||
| 2058 | /**  | 
            ||
| 2059 | * Cascades a refresh operation to associated entities.  | 
            ||
| 2060 | *  | 
            ||
| 2061 | * @param object $entity  | 
            ||
| 2062 | * @param array $visited  | 
            ||
| 2063 | *  | 
            ||
| 2064 | * @return void  | 
            ||
| 2065 | */  | 
            ||
| 2066 | 17 | View Code Duplication | private function cascadeRefresh($entity, array &$visited)  | 
            
| 2067 |     { | 
            ||
| 2068 | 17 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 2069 | |||
| 2070 | 17 | $associationMappings = array_filter(  | 
            |
| 2071 | 17 | $class->associationMappings,  | 
            |
| 2072 |             function ($assoc) { return $assoc['isCascadeRefresh']; } | 
            ||
| 2073 | );  | 
            ||
| 2074 | |||
| 2075 | 17 |         foreach ($associationMappings as $assoc) { | 
            |
| 2076 | 5 | $relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);  | 
            |
| 2077 | |||
| 2078 |             switch (true) { | 
            ||
| 2079 | 5 | case ($relatedEntities instanceof PersistentCollection):  | 
            |
| 2080 | // Unwrap so that foreach() does not initialize  | 
            ||
| 2081 | 5 | $relatedEntities = $relatedEntities->unwrap();  | 
            |
| 2082 | // break; is commented intentionally!  | 
            ||
| 2083 | |||
| 2084 | case ($relatedEntities instanceof Collection):  | 
            ||
| 2085 | case (is_array($relatedEntities)):  | 
            ||
| 2086 | 5 |                     foreach ($relatedEntities as $relatedEntity) { | 
            |
| 2087 | $this->doRefresh($relatedEntity, $visited);  | 
            ||
| 2088 | }  | 
            ||
| 2089 | 5 | break;  | 
            |
| 2090 | |||
| 2091 | case ($relatedEntities !== null):  | 
            ||
| 2092 | $this->doRefresh($relatedEntities, $visited);  | 
            ||
| 2093 | break;  | 
            ||
| 2094 | |||
| 2095 | 5 | default:  | 
            |
| 2096 | // Do nothing  | 
            ||
| 2097 | }  | 
            ||
| 2098 | }  | 
            ||
| 2099 | 17 | }  | 
            |
| 2100 | |||
| 2101 | /**  | 
            ||
| 2102 | * Cascades a detach operation to associated entities.  | 
            ||
| 2103 | *  | 
            ||
| 2104 | * @param object $entity  | 
            ||
| 2105 | * @param array $visited  | 
            ||
| 2106 | *  | 
            ||
| 2107 | * @return void  | 
            ||
| 2108 | */  | 
            ||
| 2109 | 14 | View Code Duplication | private function cascadeDetach($entity, array &$visited)  | 
            
| 2110 |     { | 
            ||
| 2111 | 14 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 2112 | |||
| 2113 | 14 | $associationMappings = array_filter(  | 
            |
| 2114 | 14 | $class->associationMappings,  | 
            |
| 2115 |             function ($assoc) { return $assoc['isCascadeDetach']; } | 
            ||
| 2116 | );  | 
            ||
| 2117 | |||
| 2118 | 14 |         foreach ($associationMappings as $assoc) { | 
            |
| 2119 | 3 | $relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);  | 
            |
| 2120 | |||
| 2121 |             switch (true) { | 
            ||
| 2122 | 3 | case ($relatedEntities instanceof PersistentCollection):  | 
            |
| 2123 | // Unwrap so that foreach() does not initialize  | 
            ||
| 2124 | 2 | $relatedEntities = $relatedEntities->unwrap();  | 
            |
| 2125 | // break; is commented intentionally!  | 
            ||
| 2126 | |||
| 2127 | 1 | case ($relatedEntities instanceof Collection):  | 
            |
| 2128 | case (is_array($relatedEntities)):  | 
            ||
| 2129 | 3 |                     foreach ($relatedEntities as $relatedEntity) { | 
            |
| 2130 | 1 | $this->doDetach($relatedEntity, $visited);  | 
            |
| 2131 | }  | 
            ||
| 2132 | 3 | break;  | 
            |
| 2133 | |||
| 2134 | case ($relatedEntities !== null):  | 
            ||
| 2135 | $this->doDetach($relatedEntities, $visited);  | 
            ||
| 2136 | break;  | 
            ||
| 2137 | |||
| 2138 | 3 | default:  | 
            |
| 2139 | // Do nothing  | 
            ||
| 2140 | }  | 
            ||
| 2141 | }  | 
            ||
| 2142 | 14 | }  | 
            |
| 2143 | |||
| 2144 | /**  | 
            ||
| 2145 | * Cascades a merge operation to associated entities.  | 
            ||
| 2146 | *  | 
            ||
| 2147 | * @param object $entity  | 
            ||
| 2148 | * @param object $managedCopy  | 
            ||
| 2149 | * @param array $visited  | 
            ||
| 2150 | *  | 
            ||
| 2151 | * @return void  | 
            ||
| 2152 | */  | 
            ||
| 2153 | 41 | private function cascadeMerge($entity, $managedCopy, array &$visited)  | 
            |
| 2154 |     { | 
            ||
| 2155 | 41 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 2156 | |||
| 2157 | 41 | $associationMappings = array_filter(  | 
            |
| 2158 | 41 | $class->associationMappings,  | 
            |
| 2159 |             function ($assoc) { return $assoc['isCascadeMerge']; } | 
            ||
| 2160 | );  | 
            ||
| 2161 | |||
| 2162 | 41 |         foreach ($associationMappings as $assoc) { | 
            |
| 2163 | 16 | $relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);  | 
            |
| 2164 | |||
| 2165 | 16 |             if ($relatedEntities instanceof Collection) { | 
            |
| 2166 | 10 |                 if ($relatedEntities === $class->reflFields[$assoc['fieldName']]->getValue($managedCopy)) { | 
            |
| 2167 | 1 | continue;  | 
            |
| 2168 | }  | 
            ||
| 2169 | |||
| 2170 | 9 |                 if ($relatedEntities instanceof PersistentCollection) { | 
            |
| 2171 | // Unwrap so that foreach() does not initialize  | 
            ||
| 2172 | 5 | $relatedEntities = $relatedEntities->unwrap();  | 
            |
| 2173 | }  | 
            ||
| 2174 | |||
| 2175 | 9 |                 foreach ($relatedEntities as $relatedEntity) { | 
            |
| 2176 | 9 | $this->doMerge($relatedEntity, $visited, $managedCopy, $assoc);  | 
            |
| 2177 | }  | 
            ||
| 2178 | 7 |             } else if ($relatedEntities !== null) { | 
            |
| 2179 | 15 | $this->doMerge($relatedEntities, $visited, $managedCopy, $assoc);  | 
            |
| 2180 | }  | 
            ||
| 2181 | }  | 
            ||
| 2182 | 41 | }  | 
            |
| 2183 | |||
| 2184 | /**  | 
            ||
| 2185 | * Cascades the save operation to associated entities.  | 
            ||
| 2186 | *  | 
            ||
| 2187 | * @param object $entity  | 
            ||
| 2188 | * @param array $visited  | 
            ||
| 2189 | *  | 
            ||
| 2190 | * @return void  | 
            ||
| 2191 | */  | 
            ||
| 2192 | 1056 | private function cascadePersist($entity, array &$visited)  | 
            |
| 2193 |     { | 
            ||
| 2194 | 1056 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 2195 | |||
| 2196 | 1056 | $associationMappings = array_filter(  | 
            |
| 2197 | 1056 | $class->associationMappings,  | 
            |
| 2198 |             function ($assoc) { return $assoc['isCascadePersist']; } | 
            ||
| 2199 | );  | 
            ||
| 2200 | |||
| 2201 | 1056 |         foreach ($associationMappings as $assoc) { | 
            |
| 2202 | 660 | $relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);  | 
            |
| 2203 | |||
| 2204 |             switch (true) { | 
            ||
| 2205 | 660 | case ($relatedEntities instanceof PersistentCollection):  | 
            |
| 2206 | // Unwrap so that foreach() does not initialize  | 
            ||
| 2207 | 21 | $relatedEntities = $relatedEntities->unwrap();  | 
            |
| 2208 | // break; is commented intentionally!  | 
            ||
| 2209 | |||
| 2210 | 660 | case ($relatedEntities instanceof Collection):  | 
            |
| 2211 | 599 | case (is_array($relatedEntities)):  | 
            |
| 2212 | 564 |                     if (($assoc['type'] & ClassMetadata::TO_MANY) <= 0) { | 
            |
| 2213 | 3 | throw ORMInvalidArgumentException::invalidAssociation(  | 
            |
| 2214 | 3 | $this->em->getClassMetadata($assoc['targetEntity']),  | 
            |
| 2215 | 3 | $assoc,  | 
            |
| 2216 | 3 | $relatedEntities  | 
            |
| 2217 | );  | 
            ||
| 2218 | }  | 
            ||
| 2219 | |||
| 2220 | 561 |                     foreach ($relatedEntities as $relatedEntity) { | 
            |
| 2221 | 282 | $this->doPersist($relatedEntity, $visited);  | 
            |
| 2222 | }  | 
            ||
| 2223 | |||
| 2224 | 561 | break;  | 
            |
| 2225 | |||
| 2226 | 589 | case ($relatedEntities !== null):  | 
            |
| 2227 | 251 |                     if (! $relatedEntities instanceof $assoc['targetEntity']) { | 
            |
| 2228 | 4 | throw ORMInvalidArgumentException::invalidAssociation(  | 
            |
| 2229 | 4 | $this->em->getClassMetadata($assoc['targetEntity']),  | 
            |
| 2230 | 4 | $assoc,  | 
            |
| 2231 | 4 | $relatedEntities  | 
            |
| 2232 | );  | 
            ||
| 2233 | }  | 
            ||
| 2234 | |||
| 2235 | 247 | $this->doPersist($relatedEntities, $visited);  | 
            |
| 2236 | 247 | break;  | 
            |
| 2237 | |||
| 2238 | 654 | default:  | 
            |
| 2239 | // Do nothing  | 
            ||
| 2240 | }  | 
            ||
| 2241 | }  | 
            ||
| 2242 | 1049 | }  | 
            |
| 2243 | |||
| 2244 | /**  | 
            ||
| 2245 | * Cascades the delete operation to associated entities.  | 
            ||
| 2246 | *  | 
            ||
| 2247 | * @param object $entity  | 
            ||
| 2248 | * @param array $visited  | 
            ||
| 2249 | *  | 
            ||
| 2250 | * @return void  | 
            ||
| 2251 | */  | 
            ||
| 2252 | 65 | private function cascadeRemove($entity, array &$visited)  | 
            |
| 2253 |     { | 
            ||
| 2254 | 65 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 2255 | |||
| 2256 | 65 | $associationMappings = array_filter(  | 
            |
| 2257 | 65 | $class->associationMappings,  | 
            |
| 2258 |             function ($assoc) { return $assoc['isCascadeRemove']; } | 
            ||
| 2259 | );  | 
            ||
| 2260 | |||
| 2261 | 65 | $entitiesToCascade = [];  | 
            |
| 2262 | |||
| 2263 | 65 |         foreach ($associationMappings as $assoc) { | 
            |
| 2264 | 26 |             if ($entity instanceof Proxy && !$entity->__isInitialized__) { | 
            |
| 2265 | 6 | $entity->__load();  | 
            |
| 2266 | }  | 
            ||
| 2267 | |||
| 2268 | 26 | $relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);  | 
            |
| 2269 | |||
| 2270 |             switch (true) { | 
            ||
| 2271 | 26 | case ($relatedEntities instanceof Collection):  | 
            |
| 2272 | 19 | case (is_array($relatedEntities)):  | 
            |
| 2273 | // If its a PersistentCollection initialization is intended! No unwrap!  | 
            ||
| 2274 | 20 |                     foreach ($relatedEntities as $relatedEntity) { | 
            |
| 2275 | 10 | $entitiesToCascade[] = $relatedEntity;  | 
            |
| 2276 | }  | 
            ||
| 2277 | 20 | break;  | 
            |
| 2278 | |||
| 2279 | 19 | case ($relatedEntities !== null):  | 
            |
| 2280 | 7 | $entitiesToCascade[] = $relatedEntities;  | 
            |
| 2281 | 7 | break;  | 
            |
| 2282 | |||
| 2283 | 26 | default:  | 
            |
| 2284 | // Do nothing  | 
            ||
| 2285 | }  | 
            ||
| 2286 | }  | 
            ||
| 2287 | |||
| 2288 | 65 |         foreach ($entitiesToCascade as $relatedEntity) { | 
            |
| 2289 | 16 | $this->doRemove($relatedEntity, $visited);  | 
            |
| 2290 | }  | 
            ||
| 2291 | 65 | }  | 
            |
| 2292 | |||
| 2293 | /**  | 
            ||
| 2294 | * Acquire a lock on the given entity.  | 
            ||
| 2295 | *  | 
            ||
| 2296 | * @param object $entity  | 
            ||
| 2297 | * @param int $lockMode  | 
            ||
| 2298 | * @param int $lockVersion  | 
            ||
| 2299 | *  | 
            ||
| 2300 | * @return void  | 
            ||
| 2301 | *  | 
            ||
| 2302 | * @throws ORMInvalidArgumentException  | 
            ||
| 2303 | * @throws TransactionRequiredException  | 
            ||
| 2304 | * @throws OptimisticLockException  | 
            ||
| 2305 | */  | 
            ||
| 2306 | 10 | public function lock($entity, $lockMode, $lockVersion = null)  | 
            |
| 2307 |     { | 
            ||
| 2308 | 10 |         if ($entity === null) { | 
            |
| 2309 | 1 |             throw new \InvalidArgumentException("No entity passed to UnitOfWork#lock()."); | 
            |
| 2310 | }  | 
            ||
| 2311 | |||
| 2312 | 9 |         if ($this->getEntityState($entity, self::STATE_DETACHED) != self::STATE_MANAGED) { | 
            |
| 2313 | 1 | throw ORMInvalidArgumentException::entityNotManaged($entity);  | 
            |
| 2314 | }  | 
            ||
| 2315 | |||
| 2316 | 8 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 2317 | |||
| 2318 |         switch (true) { | 
            ||
| 2319 | 8 | case LockMode::OPTIMISTIC === $lockMode:  | 
            |
| 2320 | 6 |                 if ( ! $class->isVersioned) { | 
            |
| 2321 | 2 | throw OptimisticLockException::notVersioned($class->name);  | 
            |
| 2322 | }  | 
            ||
| 2323 | |||
| 2324 | 4 |                 if ($lockVersion === null) { | 
            |
| 2325 | return;  | 
            ||
| 2326 | }  | 
            ||
| 2327 | |||
| 2328 | 4 |                 if ($entity instanceof Proxy && !$entity->__isInitialized__) { | 
            |
| 2329 | 1 | $entity->__load();  | 
            |
| 2330 | }  | 
            ||
| 2331 | |||
| 2332 | 4 | $entityVersion = $class->reflFields[$class->versionField]->getValue($entity);  | 
            |
| 2333 | |||
| 2334 | 4 |                 if ($entityVersion != $lockVersion) { | 
            |
| 2335 | 2 | throw OptimisticLockException::lockFailedVersionMismatch($entity, $lockVersion, $entityVersion);  | 
            |
| 2336 | }  | 
            ||
| 2337 | |||
| 2338 | 2 | break;  | 
            |
| 2339 | |||
| 2340 | 2 | case LockMode::NONE === $lockMode:  | 
            |
| 2341 | 2 | case LockMode::PESSIMISTIC_READ === $lockMode:  | 
            |
| 2342 | 1 | case LockMode::PESSIMISTIC_WRITE === $lockMode:  | 
            |
| 2343 | 2 |                 if (!$this->em->getConnection()->isTransactionActive()) { | 
            |
| 2344 | 2 | throw TransactionRequiredException::transactionRequired();  | 
            |
| 2345 | }  | 
            ||
| 2346 | |||
| 2347 | $oid = spl_object_hash($entity);  | 
            ||
| 2348 | |||
| 2349 | $this->getEntityPersister($class->name)->lock(  | 
            ||
| 2350 | array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]),  | 
            ||
| 2351 | $lockMode  | 
            ||
| 2352 | );  | 
            ||
| 2353 | break;  | 
            ||
| 2354 | |||
| 2355 | default:  | 
            ||
| 2356 | // Do nothing  | 
            ||
| 2357 | }  | 
            ||
| 2358 | 2 | }  | 
            |
| 2359 | |||
| 2360 | /**  | 
            ||
| 2361 | * Gets the CommitOrderCalculator used by the UnitOfWork to order commits.  | 
            ||
| 2362 | *  | 
            ||
| 2363 | * @return \Doctrine\ORM\Internal\CommitOrderCalculator  | 
            ||
| 2364 | */  | 
            ||
| 2365 | 1030 | public function getCommitOrderCalculator()  | 
            |
| 2366 |     { | 
            ||
| 2367 | 1030 | return new Internal\CommitOrderCalculator();  | 
            |
| 2368 | }  | 
            ||
| 2369 | |||
| 2370 | /**  | 
            ||
| 2371 | * Clears the UnitOfWork.  | 
            ||
| 2372 | *  | 
            ||
| 2373 | * @param string|null $entityName if given, only entities of this type will get detached.  | 
            ||
| 2374 | *  | 
            ||
| 2375 | * @return void  | 
            ||
| 2376 | *  | 
            ||
| 2377 | * @throws ORMInvalidArgumentException if an invalid entity name is given  | 
            ||
| 2378 | */  | 
            ||
| 2379 | 1252 | public function clear($entityName = null)  | 
            |
| 2380 |     { | 
            ||
| 2381 | 1252 |         if ($entityName === null) { | 
            |
| 2382 | 1250 | $this->identityMap =  | 
            |
| 2383 | 1250 | $this->entityIdentifiers =  | 
            |
| 2384 | 1250 | $this->originalEntityData =  | 
            |
| 2385 | 1250 | $this->entityChangeSets =  | 
            |
| 2386 | 1250 | $this->entityStates =  | 
            |
| 2387 | 1250 | $this->scheduledForSynchronization =  | 
            |
| 2388 | 1250 | $this->entityInsertions =  | 
            |
| 2389 | 1250 | $this->entityUpdates =  | 
            |
| 2390 | 1250 | $this->entityDeletions =  | 
            |
| 2391 | 1250 | $this->collectionDeletions =  | 
            |
| 2392 | 1250 | $this->collectionUpdates =  | 
            |
| 2393 | 1250 | $this->extraUpdates =  | 
            |
| 2394 | 1250 | $this->readOnlyObjects =  | 
            |
| 2395 | 1250 | $this->visitedCollections =  | 
            |
| 2396 | 1250 | $this->orphanRemovals = [];  | 
            |
| 2397 |         } else { | 
            ||
| 2398 | 4 | $this->clearIdentityMapForEntityName($entityName);  | 
            |
| 2399 | 4 | $this->clearEntityInsertionsForEntityName($entityName);  | 
            |
| 2400 | }  | 
            ||
| 2401 | |||
| 2402 | 1252 |         if ($this->evm->hasListeners(Events::onClear)) { | 
            |
| 2403 | 5 | $this->evm->dispatchEvent(Events::onClear, new Event\OnClearEventArgs($this->em, $entityName));  | 
            |
| 2404 | }  | 
            ||
| 2405 | 1252 | }  | 
            |
| 2406 | |||
| 2407 | /**  | 
            ||
| 2408 | * INTERNAL:  | 
            ||
| 2409 | * Schedules an orphaned entity for removal. The remove() operation will be  | 
            ||
| 2410 | * invoked on that entity at the beginning of the next commit of this  | 
            ||
| 2411 | * UnitOfWork.  | 
            ||
| 2412 | *  | 
            ||
| 2413 | * @ignore  | 
            ||
| 2414 | *  | 
            ||
| 2415 | * @param object $entity  | 
            ||
| 2416 | *  | 
            ||
| 2417 | * @return void  | 
            ||
| 2418 | */  | 
            ||
| 2419 | 17 | public function scheduleOrphanRemoval($entity)  | 
            |
| 2420 |     { | 
            ||
| 2421 | 17 | $this->orphanRemovals[spl_object_hash($entity)] = $entity;  | 
            |
| 2422 | 17 | }  | 
            |
| 2423 | |||
| 2424 | /**  | 
            ||
| 2425 | * INTERNAL:  | 
            ||
| 2426 | * Cancels a previously scheduled orphan removal.  | 
            ||
| 2427 | *  | 
            ||
| 2428 | * @ignore  | 
            ||
| 2429 | *  | 
            ||
| 2430 | * @param object $entity  | 
            ||
| 2431 | *  | 
            ||
| 2432 | * @return void  | 
            ||
| 2433 | */  | 
            ||
| 2434 | 114 | public function cancelOrphanRemoval($entity)  | 
            |
| 2435 |     { | 
            ||
| 2436 | 114 | unset($this->orphanRemovals[spl_object_hash($entity)]);  | 
            |
| 2437 | 114 | }  | 
            |
| 2438 | |||
| 2439 | /**  | 
            ||
| 2440 | * INTERNAL:  | 
            ||
| 2441 | * Schedules a complete collection for removal when this UnitOfWork commits.  | 
            ||
| 2442 | *  | 
            ||
| 2443 | * @param PersistentCollection $coll  | 
            ||
| 2444 | *  | 
            ||
| 2445 | * @return void  | 
            ||
| 2446 | */  | 
            ||
| 2447 | 14 | public function scheduleCollectionDeletion(PersistentCollection $coll)  | 
            |
| 2448 |     { | 
            ||
| 2449 | 14 | $coid = spl_object_hash($coll);  | 
            |
| 2450 | |||
| 2451 | // TODO: if $coll is already scheduled for recreation ... what to do?  | 
            ||
| 2452 | // Just remove $coll from the scheduled recreations?  | 
            ||
| 2453 | 14 | unset($this->collectionUpdates[$coid]);  | 
            |
| 2454 | |||
| 2455 | 14 | $this->collectionDeletions[$coid] = $coll;  | 
            |
| 2456 | 14 | }  | 
            |
| 2457 | |||
| 2458 | /**  | 
            ||
| 2459 | * @param PersistentCollection $coll  | 
            ||
| 2460 | *  | 
            ||
| 2461 | * @return bool  | 
            ||
| 2462 | */  | 
            ||
| 2463 | public function isCollectionScheduledForDeletion(PersistentCollection $coll)  | 
            ||
| 2464 |     { | 
            ||
| 2465 | return isset($this->collectionDeletions[spl_object_hash($coll)]);  | 
            ||
| 2466 | }  | 
            ||
| 2467 | |||
| 2468 | /**  | 
            ||
| 2469 | * @param ClassMetadata $class  | 
            ||
| 2470 | *  | 
            ||
| 2471 | * @return \Doctrine\Common\Persistence\ObjectManagerAware|object  | 
            ||
| 2472 | */  | 
            ||
| 2473 | 696 | private function newInstance($class)  | 
            |
| 2474 |     { | 
            ||
| 2475 | 696 | $entity = $class->newInstance();  | 
            |
| 2476 | |||
| 2477 | 696 |         if ($entity instanceof \Doctrine\Common\Persistence\ObjectManagerAware) { | 
            |
| 2478 | 4 | $entity->injectObjectManager($this->em, $class);  | 
            |
| 2479 | }  | 
            ||
| 2480 | |||
| 2481 | 696 | return $entity;  | 
            |
| 2482 | }  | 
            ||
| 2483 | |||
| 2484 | /**  | 
            ||
| 2485 | * INTERNAL:  | 
            ||
| 2486 | * Creates an entity. Used for reconstitution of persistent entities.  | 
            ||
| 2487 | *  | 
            ||
| 2488 | * Internal note: Highly performance-sensitive method.  | 
            ||
| 2489 | *  | 
            ||
| 2490 | * @ignore  | 
            ||
| 2491 | *  | 
            ||
| 2492 | * @param string $className The name of the entity class.  | 
            ||
| 2493 | * @param array $data The data for the entity.  | 
            ||
| 2494 | * @param array $hints Any hints to account for during reconstitution/lookup of the entity.  | 
            ||
| 2495 | *  | 
            ||
| 2496 | * @return object The managed entity instance.  | 
            ||
| 2497 | *  | 
            ||
| 2498 | * @todo Rename: getOrCreateEntity  | 
            ||
| 2499 | */  | 
            ||
| 2500 | 833 | public function createEntity($className, array $data, &$hints = [])  | 
            |
| 2501 |     { | 
            ||
| 2502 | 833 | $class = $this->em->getClassMetadata($className);  | 
            |
| 2503 | //$isReadOnly = isset($hints[Query::HINT_READ_ONLY]);  | 
            ||
| 2504 | |||
| 2505 | 833 | $id = $this->identifierFlattener->flattenIdentifier($class, $data);  | 
            |
| 2506 | 833 | $idHash = $this->hashIdentifier($id);  | 
            |
| 2507 | |||
| 2508 | 833 |         if (isset($this->identityMap[$class->rootEntityName][$idHash])) { | 
            |
| 2509 | 317 | $entity = $this->identityMap[$class->rootEntityName][$idHash];  | 
            |
| 2510 | 317 | $oid = spl_object_hash($entity);  | 
            |
| 2511 | |||
| 2512 | if (  | 
            ||
| 2513 | 317 | isset($hints[Query::HINT_REFRESH])  | 
            |
| 2514 | 317 | && isset($hints[Query::HINT_REFRESH_ENTITY])  | 
            |
| 2515 | 317 | && ($unmanagedProxy = $hints[Query::HINT_REFRESH_ENTITY]) !== $entity  | 
            |
| 2516 | 317 | && $unmanagedProxy instanceof Proxy  | 
            |
| 2517 | 317 | && $this->isIdentifierEquals($unmanagedProxy, $entity)  | 
            |
| 2518 |             ) { | 
            ||
| 2519 | // DDC-1238 - we have a managed instance, but it isn't the provided one.  | 
            ||
| 2520 | // Therefore we clear its identifier. Also, we must re-fetch metadata since the  | 
            ||
| 2521 | // refreshed object may be anything  | 
            ||
| 2522 | |||
| 2523 | 2 |                 foreach ($class->identifier as $fieldName) { | 
            |
| 2524 | 2 | $class->reflFields[$fieldName]->setValue($unmanagedProxy, null);  | 
            |
| 2525 | }  | 
            ||
| 2526 | |||
| 2527 | 2 | return $unmanagedProxy;  | 
            |
| 2528 | }  | 
            ||
| 2529 | |||
| 2530 | 315 |             if ($entity instanceof Proxy && ! $entity->__isInitialized()) { | 
            |
| 2531 | 23 | $entity->__setInitialized(true);  | 
            |
| 2532 | |||
| 2533 | 23 | $overrideLocalValues = true;  | 
            |
| 2534 | |||
| 2535 | 23 |                 if ($entity instanceof NotifyPropertyChanged) { | 
            |
| 2536 | 23 | $entity->addPropertyChangedListener($this);  | 
            |
| 2537 | }  | 
            ||
| 2538 |             } else { | 
            ||
| 2539 | 294 | $overrideLocalValues = isset($hints[Query::HINT_REFRESH]);  | 
            |
| 2540 | |||
| 2541 | // If only a specific entity is set to refresh, check that it's the one  | 
            ||
| 2542 | 294 |                 if (isset($hints[Query::HINT_REFRESH_ENTITY])) { | 
            |
| 2543 | 72 | $overrideLocalValues = $hints[Query::HINT_REFRESH_ENTITY] === $entity;  | 
            |
| 2544 | }  | 
            ||
| 2545 | }  | 
            ||
| 2546 | |||
| 2547 | 315 |             if ($overrideLocalValues) { | 
            |
| 2548 | // inject ObjectManager upon refresh.  | 
            ||
| 2549 | 115 |                 if ($entity instanceof ObjectManagerAware) { | 
            |
| 2550 | 3 | $entity->injectObjectManager($this->em, $class);  | 
            |
| 2551 | }  | 
            ||
| 2552 | |||
| 2553 | 315 | $this->originalEntityData[$oid] = $data;  | 
            |
| 2554 | }  | 
            ||
| 2555 |         } else { | 
            ||
| 2556 | 691 | $entity = $this->newInstance($class);  | 
            |
| 2557 | 691 | $oid = spl_object_hash($entity);  | 
            |
| 2558 | |||
| 2559 | 691 | $this->entityIdentifiers[$oid] = $id;  | 
            |
| 2560 | 691 | $this->entityStates[$oid] = self::STATE_MANAGED;  | 
            |
| 2561 | 691 | $this->originalEntityData[$oid] = $data;  | 
            |
| 2562 | |||
| 2563 | 691 | $this->identityMap[$class->rootEntityName][$idHash] = $entity;  | 
            |
| 2564 | |||
| 2565 | 691 |             if ($entity instanceof NotifyPropertyChanged) { | 
            |
| 2566 | 2 | $entity->addPropertyChangedListener($this);  | 
            |
| 2567 | }  | 
            ||
| 2568 | |||
| 2569 | 691 | $overrideLocalValues = true;  | 
            |
| 2570 | }  | 
            ||
| 2571 | |||
| 2572 | 832 |         if ( ! $overrideLocalValues) { | 
            |
| 2573 | 223 | return $entity;  | 
            |
| 2574 | }  | 
            ||
| 2575 | |||
| 2576 | 729 |         foreach ($data as $field => $value) { | 
            |
| 2577 | 729 |             if (isset($class->fieldMappings[$field])) { | 
            |
| 2578 | 729 | $class->reflFields[$field]->setValue($entity, $value);  | 
            |
| 2579 | }  | 
            ||
| 2580 | }  | 
            ||
| 2581 | |||
| 2582 | // Loading the entity right here, if its in the eager loading map get rid of it there.  | 
            ||
| 2583 | 729 | unset($this->eagerLoadingEntities[$class->rootEntityName][$idHash]);  | 
            |
| 2584 | |||
| 2585 | 729 |         if (isset($this->eagerLoadingEntities[$class->rootEntityName]) && ! $this->eagerLoadingEntities[$class->rootEntityName]) { | 
            |
| 2586 | unset($this->eagerLoadingEntities[$class->rootEntityName]);  | 
            ||
| 2587 | }  | 
            ||
| 2588 | |||
| 2589 | // Properly initialize any unfetched associations, if partial objects are not allowed.  | 
            ||
| 2590 | 729 |         if (isset($hints[Query::HINT_FORCE_PARTIAL_LOAD])) { | 
            |
| 2591 | 34 | return $entity;  | 
            |
| 2592 | }  | 
            ||
| 2593 | |||
| 2594 | 695 |         foreach ($class->associationMappings as $field => $assoc) { | 
            |
| 2595 | // Check if the association is not among the fetch-joined associations already.  | 
            ||
| 2596 | 601 |             if (isset($hints['fetchAlias']) && isset($hints['fetched'][$hints['fetchAlias']][$field])) { | 
            |
| 2597 | 257 | continue;  | 
            |
| 2598 | }  | 
            ||
| 2599 | |||
| 2600 | 578 | $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);  | 
            |
| 2601 | |||
| 2602 |             switch (true) { | 
            ||
| 2603 | 578 | case ($assoc['type'] & ClassMetadata::TO_ONE):  | 
            |
| 2604 | 498 |                     if ( ! $assoc['isOwningSide']) { | 
            |
| 2605 | |||
| 2606 | // use the given entity association  | 
            ||
| 2607 | 67 |                         if (isset($data[$field]) && is_object($data[$field]) && isset($this->entityStates[spl_object_hash($data[$field])])) { | 
            |
| 2608 | |||
| 2609 | 3 | $this->originalEntityData[$oid][$field] = $data[$field];  | 
            |
| 2610 | |||
| 2611 | 3 | $class->reflFields[$field]->setValue($entity, $data[$field]);  | 
            |
| 2612 | 3 | $targetClass->reflFields[$assoc['mappedBy']]->setValue($data[$field], $entity);  | 
            |
| 2613 | |||
| 2614 | 3 | continue 2;  | 
            |
| 2615 | }  | 
            ||
| 2616 | |||
| 2617 | // Inverse side of x-to-one can never be lazy  | 
            ||
| 2618 | 64 | $class->reflFields[$field]->setValue($entity, $this->getEntityPersister($assoc['targetEntity'])->loadOneToOneEntity($assoc, $entity));  | 
            |
| 2619 | |||
| 2620 | 64 | continue 2;  | 
            |
| 2621 | }  | 
            ||
| 2622 | |||
| 2623 | // use the entity association  | 
            ||
| 2624 | 498 |                     if (isset($data[$field]) && is_object($data[$field]) && isset($this->entityStates[spl_object_hash($data[$field])])) { | 
            |
| 2625 | 38 | $class->reflFields[$field]->setValue($entity, $data[$field]);  | 
            |
| 2626 | 38 | $this->originalEntityData[$oid][$field] = $data[$field];  | 
            |
| 2627 | |||
| 2628 | 38 | continue;  | 
            |
| 2629 | }  | 
            ||
| 2630 | |||
| 2631 | 491 | $associatedId = [];  | 
            |
| 2632 | |||
| 2633 | // TODO: Is this even computed right in all cases of composite keys?  | 
            ||
| 2634 | 491 |                     foreach ($assoc['targetToSourceKeyColumns'] as $targetColumn => $srcColumn) { | 
            |
| 2635 | 491 | $joinColumnValue = isset($data[$srcColumn]) ? $data[$srcColumn] : null;  | 
            |
| 2636 | |||
| 2637 | 491 |                         if ($joinColumnValue !== null) { | 
            |
| 2638 | 296 |                             if ($targetClass->containsForeignIdentifier) { | 
            |
| 2639 | 11 | $associatedId[$targetClass->getFieldForColumn($targetColumn)] = $joinColumnValue;  | 
            |
| 2640 |                             } else { | 
            ||
| 2641 | 296 | $associatedId[$targetClass->fieldNames[$targetColumn]] = $joinColumnValue;  | 
            |
| 2642 | }  | 
            ||
| 2643 | 289 | } elseif ($targetClass->containsForeignIdentifier  | 
            |
| 2644 | 289 | && in_array($targetClass->getFieldForColumn($targetColumn), $targetClass->identifier, true)  | 
            |
| 2645 |                         ) { | 
            ||
| 2646 | // the missing key is part of target's entity primary key  | 
            ||
| 2647 | 7 | $associatedId = [];  | 
            |
| 2648 | 491 | break;  | 
            |
| 2649 | }  | 
            ||
| 2650 | }  | 
            ||
| 2651 | |||
| 2652 | 491 |                     if ( ! $associatedId) { | 
            |
| 2653 | // Foreign key is NULL  | 
            ||
| 2654 | 289 | $class->reflFields[$field]->setValue($entity, null);  | 
            |
| 2655 | 289 | $this->originalEntityData[$oid][$field] = null;  | 
            |
| 2656 | |||
| 2657 | 289 | continue;  | 
            |
| 2658 | }  | 
            ||
| 2659 | |||
| 2660 | 296 |                     if ( ! isset($hints['fetchMode'][$class->name][$field])) { | 
            |
| 2661 | 293 | $hints['fetchMode'][$class->name][$field] = $assoc['fetch'];  | 
            |
| 2662 | }  | 
            ||
| 2663 | |||
| 2664 | // Foreign key is set  | 
            ||
| 2665 | // Check identity map first  | 
            ||
| 2666 | // FIXME: Can break easily with composite keys if join column values are in  | 
            ||
| 2667 | // wrong order. The correct order is the one in ClassMetadata#identifier.  | 
            ||
| 2668 | 296 | $relatedIdHash = $this->hashIdentifier($associatedId);  | 
            |
| 2669 | |||
| 2670 |                     switch (true) { | 
            ||
| 2671 | 296 | case (isset($this->identityMap[$targetClass->rootEntityName][$relatedIdHash])):  | 
            |
| 2672 | 171 | $newValue = $this->identityMap[$targetClass->rootEntityName][$relatedIdHash];  | 
            |
| 2673 | |||
| 2674 | // If this is an uninitialized proxy, we are deferring eager loads,  | 
            ||
| 2675 | // this association is marked as eager fetch, and its an uninitialized proxy (wtf!)  | 
            ||
| 2676 | // then we can append this entity for eager loading!  | 
            ||
| 2677 | 171 | if ($hints['fetchMode'][$class->name][$field] == ClassMetadata::FETCH_EAGER &&  | 
            |
| 2678 | 171 | isset($hints[self::HINT_DEFEREAGERLOAD]) &&  | 
            |
| 2679 | 171 | !$targetClass->isIdentifierComposite &&  | 
            |
| 2680 | 171 | $newValue instanceof Proxy &&  | 
            |
| 2681 | 171 |                                 $newValue->__isInitialized__ === false) { | 
            |
| 2682 | |||
| 2683 | $this->eagerLoadingEntities[$targetClass->rootEntityName][$relatedIdHash] = current($associatedId);  | 
            ||
| 2684 | }  | 
            ||
| 2685 | |||
| 2686 | 171 | break;  | 
            |
| 2687 | |||
| 2688 | 201 | case ($targetClass->subClasses):  | 
            |
| 2689 | // If it might be a subtype, it can not be lazy. There isn't even  | 
            ||
| 2690 | // a way to solve this with deferred eager loading, which means putting  | 
            ||
| 2691 | // an entity with subclasses at a *-to-one location is really bad! (performance-wise)  | 
            ||
| 2692 | 32 | $newValue = $this->getEntityPersister($assoc['targetEntity'])->loadOneToOneEntity($assoc, $entity, $associatedId);  | 
            |
| 2693 | 32 | break;  | 
            |
| 2694 | |||
| 2695 | default:  | 
            ||
| 2696 |                             switch (true) { | 
            ||
| 2697 | // We are negating the condition here. Other cases will assume it is valid!  | 
            ||
| 2698 | 171 | case ($hints['fetchMode'][$class->name][$field] !== ClassMetadata::FETCH_EAGER):  | 
            |
| 2699 | 164 | $newValue = $this->em->getProxyFactory()->getProxy($assoc['targetEntity'], $associatedId);  | 
            |
| 2700 | 164 | break;  | 
            |
| 2701 | |||
| 2702 | // Deferred eager load only works for single identifier classes  | 
            ||
| 2703 | 7 | case (isset($hints[self::HINT_DEFEREAGERLOAD]) && ! $targetClass->isIdentifierComposite):  | 
            |
| 2704 | // TODO: Is there a faster approach?  | 
            ||
| 2705 | 7 | $this->eagerLoadingEntities[$targetClass->rootEntityName][$relatedIdHash] = current($associatedId);  | 
            |
| 2706 | |||
| 2707 | 7 | $newValue = $this->em->getProxyFactory()->getProxy($assoc['targetEntity'], $associatedId);  | 
            |
| 2708 | 7 | break;  | 
            |
| 2709 | |||
| 2710 | default:  | 
            ||
| 2711 | // TODO: This is very imperformant, ignore it?  | 
            ||
| 2712 | $newValue = $this->em->find($assoc['targetEntity'], $associatedId);  | 
            ||
| 2713 | break;  | 
            ||
| 2714 | }  | 
            ||
| 2715 | |||
| 2716 | // PERF: Inlined & optimized code from UnitOfWork#registerManaged()  | 
            ||
| 2717 | 171 | $newValueOid = spl_object_hash($newValue);  | 
            |
| 2718 | 171 | $this->entityIdentifiers[$newValueOid] = $associatedId;  | 
            |
| 2719 | 171 | $this->identityMap[$targetClass->rootEntityName][$relatedIdHash] = $newValue;  | 
            |
| 2720 | |||
| 2721 | if (  | 
            ||
| 2722 | 171 | $newValue instanceof NotifyPropertyChanged &&  | 
            |
| 2723 | 171 | ( ! $newValue instanceof Proxy || $newValue->__isInitialized())  | 
            |
| 2724 |                             ) { | 
            ||
| 2725 | $newValue->addPropertyChangedListener($this);  | 
            ||
| 2726 | }  | 
            ||
| 2727 | 171 | $this->entityStates[$newValueOid] = self::STATE_MANAGED;  | 
            |
| 2728 | // make sure that when an proxy is then finally loaded, $this->originalEntityData is set also!  | 
            ||
| 2729 | 171 | break;  | 
            |
| 2730 | }  | 
            ||
| 2731 | |||
| 2732 | 296 | $this->originalEntityData[$oid][$field] = $newValue;  | 
            |
| 2733 | 296 | $class->reflFields[$field]->setValue($entity, $newValue);  | 
            |
| 2734 | |||
| 2735 | 296 |                     if ($assoc['inversedBy'] && $assoc['type'] & ClassMetadata::ONE_TO_ONE) { | 
            |
| 2736 | 57 | $inverseAssoc = $targetClass->associationMappings[$assoc['inversedBy']];  | 
            |
| 2737 | 57 | $targetClass->reflFields[$inverseAssoc['fieldName']]->setValue($newValue, $entity);  | 
            |
| 2738 | }  | 
            ||
| 2739 | |||
| 2740 | 296 | break;  | 
            |
| 2741 | |||
| 2742 | default:  | 
            ||
| 2743 | // Ignore if its a cached collection  | 
            ||
| 2744 | 492 |                     if (isset($hints[Query::HINT_CACHE_ENABLED]) && $class->getFieldValue($entity, $field) instanceof PersistentCollection) { | 
            |
| 2745 | break;  | 
            ||
| 2746 | }  | 
            ||
| 2747 | |||
| 2748 | // use the given collection  | 
            ||
| 2749 | 492 |                     if (isset($data[$field]) && $data[$field] instanceof PersistentCollection) { | 
            |
| 2750 | |||
| 2751 | 3 | $data[$field]->setOwner($entity, $assoc);  | 
            |
| 2752 | |||
| 2753 | 3 | $class->reflFields[$field]->setValue($entity, $data[$field]);  | 
            |
| 2754 | 3 | $this->originalEntityData[$oid][$field] = $data[$field];  | 
            |
| 2755 | |||
| 2756 | 3 | break;  | 
            |
| 2757 | }  | 
            ||
| 2758 | |||
| 2759 | // Inject collection  | 
            ||
| 2760 | 492 | $pColl = new PersistentCollection($this->em, $targetClass, new ArrayCollection);  | 
            |
| 2761 | 492 | $pColl->setOwner($entity, $assoc);  | 
            |
| 2762 | 492 | $pColl->setInitialized(false);  | 
            |
| 2763 | |||
| 2764 | 492 | $reflField = $class->reflFields[$field];  | 
            |
| 2765 | 492 | $reflField->setValue($entity, $pColl);  | 
            |
| 2766 | |||
| 2767 | 492 |                     if ($assoc['fetch'] == ClassMetadata::FETCH_EAGER) { | 
            |
| 2768 | 4 | $this->loadCollection($pColl);  | 
            |
| 2769 | 4 | $pColl->takeSnapshot();  | 
            |
| 2770 | }  | 
            ||
| 2771 | |||
| 2772 | 492 | $this->originalEntityData[$oid][$field] = $pColl;  | 
            |
| 2773 | 578 | break;  | 
            |
| 2774 | }  | 
            ||
| 2775 | }  | 
            ||
| 2776 | |||
| 2777 | 695 |         if ($overrideLocalValues) { | 
            |
| 2778 | // defer invoking of postLoad event to hydration complete step  | 
            ||
| 2779 | 695 | $this->hydrationCompleteHandler->deferPostLoadInvoking($class, $entity);  | 
            |
| 2780 | }  | 
            ||
| 2781 | |||
| 2782 | 695 | return $entity;  | 
            |
| 2783 | }  | 
            ||
| 2784 | |||
| 2785 | /**  | 
            ||
| 2786 | * @return void  | 
            ||
| 2787 | */  | 
            ||
| 2788 | 897 | public function triggerEagerLoads()  | 
            |
| 2789 |     { | 
            ||
| 2790 | 897 |         if ( ! $this->eagerLoadingEntities) { | 
            |
| 2791 | 897 | return;  | 
            |
| 2792 | }  | 
            ||
| 2793 | |||
| 2794 | // avoid infinite recursion  | 
            ||
| 2795 | 7 | $eagerLoadingEntities = $this->eagerLoadingEntities;  | 
            |
| 2796 | 7 | $this->eagerLoadingEntities = [];  | 
            |
| 2797 | |||
| 2798 | 7 |         foreach ($eagerLoadingEntities as $entityName => $ids) { | 
            |
| 2799 | 7 |             if ( ! $ids) { | 
            |
| 2800 | continue;  | 
            ||
| 2801 | }  | 
            ||
| 2802 | |||
| 2803 | 7 | $class = $this->em->getClassMetadata($entityName);  | 
            |
| 2804 | |||
| 2805 | 7 | $this->getEntityPersister($entityName)->loadAll(  | 
            |
| 2806 | 7 | array_combine($class->identifier, [array_values($ids)])  | 
            |
| 2807 | );  | 
            ||
| 2808 | }  | 
            ||
| 2809 | 7 | }  | 
            |
| 2810 | |||
| 2811 | /**  | 
            ||
| 2812 | * Initializes (loads) an uninitialized persistent collection of an entity.  | 
            ||
| 2813 | *  | 
            ||
| 2814 | * @param \Doctrine\ORM\PersistentCollection $collection The collection to initialize.  | 
            ||
| 2815 | *  | 
            ||
| 2816 | * @return void  | 
            ||
| 2817 | *  | 
            ||
| 2818 | * @todo Maybe later move to EntityManager#initialize($proxyOrCollection). See DDC-733.  | 
            ||
| 2819 | */  | 
            ||
| 2820 | 147 | public function loadCollection(PersistentCollection $collection)  | 
            |
| 2821 |     { | 
            ||
| 2822 | 147 | $assoc = $collection->getMapping();  | 
            |
| 2823 | 147 | $persister = $this->getEntityPersister($assoc['targetEntity']);  | 
            |
| 2824 | |||
| 2825 | 147 |         switch ($assoc['type']) { | 
            |
| 2826 | 147 | case ClassMetadata::ONE_TO_MANY:  | 
            |
| 2827 | 77 | $persister->loadOneToManyCollection($assoc, $collection->getOwner(), $collection);  | 
            |
| 2828 | 77 | break;  | 
            |
| 2829 | |||
| 2830 | 84 | case ClassMetadata::MANY_TO_MANY:  | 
            |
| 2831 | 84 | $persister->loadManyToManyCollection($assoc, $collection->getOwner(), $collection);  | 
            |
| 2832 | 84 | break;  | 
            |
| 2833 | }  | 
            ||
| 2834 | |||
| 2835 | 147 | $collection->setInitialized(true);  | 
            |
| 2836 | 147 | }  | 
            |
| 2837 | |||
| 2838 | /**  | 
            ||
| 2839 | * Gets the identity map of the UnitOfWork.  | 
            ||
| 2840 | *  | 
            ||
| 2841 | * @return array  | 
            ||
| 2842 | */  | 
            ||
| 2843 | 2 | public function getIdentityMap()  | 
            |
| 2844 |     { | 
            ||
| 2845 | 2 | return $this->identityMap;  | 
            |
| 2846 | }  | 
            ||
| 2847 | |||
| 2848 | /**  | 
            ||
| 2849 | * Gets the original data of an entity. The original data is the data that was  | 
            ||
| 2850 | * present at the time the entity was reconstituted from the database.  | 
            ||
| 2851 | *  | 
            ||
| 2852 | * @param object $entity  | 
            ||
| 2853 | *  | 
            ||
| 2854 | * @return array  | 
            ||
| 2855 | */  | 
            ||
| 2856 | 120 | public function getOriginalEntityData($entity)  | 
            |
| 2857 |     { | 
            ||
| 2858 | 120 | $oid = spl_object_hash($entity);  | 
            |
| 2859 | |||
| 2860 | 120 | return isset($this->originalEntityData[$oid])  | 
            |
| 2861 | 116 | ? $this->originalEntityData[$oid]  | 
            |
| 2862 | 120 | : [];  | 
            |
| 2863 | }  | 
            ||
| 2864 | |||
| 2865 | /**  | 
            ||
| 2866 | * @ignore  | 
            ||
| 2867 | *  | 
            ||
| 2868 | * @param object $entity  | 
            ||
| 2869 | * @param array $data  | 
            ||
| 2870 | *  | 
            ||
| 2871 | * @return void  | 
            ||
| 2872 | */  | 
            ||
| 2873 | public function setOriginalEntityData($entity, array $data)  | 
            ||
| 2874 |     { | 
            ||
| 2875 | $this->originalEntityData[spl_object_hash($entity)] = $data;  | 
            ||
| 2876 | }  | 
            ||
| 2877 | |||
| 2878 | /**  | 
            ||
| 2879 | * INTERNAL:  | 
            ||
| 2880 | * Sets a property value of the original data array of an entity.  | 
            ||
| 2881 | *  | 
            ||
| 2882 | * @ignore  | 
            ||
| 2883 | *  | 
            ||
| 2884 | * @param string $oid  | 
            ||
| 2885 | * @param string $property  | 
            ||
| 2886 | * @param mixed $value  | 
            ||
| 2887 | *  | 
            ||
| 2888 | * @return void  | 
            ||
| 2889 | */  | 
            ||
| 2890 | 311 | public function setOriginalEntityProperty($oid, $property, $value)  | 
            |
| 2891 |     { | 
            ||
| 2892 | 311 | $this->originalEntityData[$oid][$property] = $value;  | 
            |
| 2893 | 311 | }  | 
            |
| 2894 | |||
| 2895 | /**  | 
            ||
| 2896 | * Gets the identifier of an entity.  | 
            ||
| 2897 | * The returned value is always an array of identifier values. If the entity  | 
            ||
| 2898 | * has a composite identifier then the identifier values are in the same  | 
            ||
| 2899 | * order as the identifier field names as returned by ClassMetadata#getIdentifierFieldNames().  | 
            ||
| 2900 | *  | 
            ||
| 2901 | * @param object $entity  | 
            ||
| 2902 | *  | 
            ||
| 2903 | * @return array The identifier values.  | 
            ||
| 2904 | */  | 
            ||
| 2905 | 857 | public function getEntityIdentifier($entity)  | 
            |
| 2906 |     { | 
            ||
| 2907 | 857 | return $this->entityIdentifiers[spl_object_hash($entity)];  | 
            |
| 2908 | }  | 
            ||
| 2909 | |||
| 2910 | /**  | 
            ||
| 2911 | * Processes an entity instance to extract their identifier values.  | 
            ||
| 2912 | *  | 
            ||
| 2913 | * @param object $entity The entity instance.  | 
            ||
| 2914 | *  | 
            ||
| 2915 | * @return mixed A scalar value.  | 
            ||
| 2916 | *  | 
            ||
| 2917 | * @throws \Doctrine\ORM\ORMInvalidArgumentException  | 
            ||
| 2918 | */  | 
            ||
| 2919 | 127 | public function getSingleIdentifierValue($entity)  | 
            |
| 2920 |     { | 
            ||
| 2921 | 127 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 2922 | |||
| 2923 | 127 |         if ($class->isIdentifierComposite) { | 
            |
| 2924 | throw ORMInvalidArgumentException::invalidCompositeIdentifier();  | 
            ||
| 2925 | }  | 
            ||
| 2926 | |||
| 2927 | 127 | $values = $this->isInIdentityMap($entity)  | 
            |
| 2928 | 114 | ? $this->getEntityIdentifier($entity)  | 
            |
| 2929 | 127 | : $class->getIdentifierValues($entity);  | 
            |
| 2930 | |||
| 2931 | 127 | return isset($values[$class->identifier[0]]) ? $values[$class->identifier[0]] : null;  | 
            |
| 2932 | }  | 
            ||
| 2933 | |||
| 2934 | /**  | 
            ||
| 2935 | * Tries to find an entity with the given identifier in the identity map of  | 
            ||
| 2936 | * this UnitOfWork.  | 
            ||
| 2937 | *  | 
            ||
| 2938 | * @param mixed $id The entity identifier to look for.  | 
            ||
| 2939 | * @param string $rootClassName The name of the root class of the mapped entity hierarchy.  | 
            ||
| 2940 | *  | 
            ||
| 2941 | * @return object|bool Returns the entity with the specified identifier if it exists in  | 
            ||
| 2942 | * this UnitOfWork, FALSE otherwise.  | 
            ||
| 2943 | */  | 
            ||
| 2944 | 548 | View Code Duplication | public function tryGetById($id, $rootClassName)  | 
            
| 2945 |     { | 
            ||
| 2946 | 548 | $idHash = $this->hashIdentifier((array) $id);  | 
            |
| 2947 | |||
| 2948 | 548 | return isset($this->identityMap[$rootClassName][$idHash])  | 
            |
| 2949 | 88 | ? $this->identityMap[$rootClassName][$idHash]  | 
            |
| 2950 | 548 | : false;  | 
            |
| 2951 | }  | 
            ||
| 2952 | |||
| 2953 | /**  | 
            ||
| 2954 | * Schedules an entity for dirty-checking at commit-time.  | 
            ||
| 2955 | *  | 
            ||
| 2956 | * @param object $entity The entity to schedule for dirty-checking.  | 
            ||
| 2957 | *  | 
            ||
| 2958 | * @return void  | 
            ||
| 2959 | *  | 
            ||
| 2960 | * @todo Rename: scheduleForSynchronization  | 
            ||
| 2961 | */  | 
            ||
| 2962 | 5 | public function scheduleForDirtyCheck($entity)  | 
            |
| 2963 |     { | 
            ||
| 2964 | 5 | $rootClassName = $this->em->getClassMetadata(get_class($entity))->rootEntityName;  | 
            |
| 2965 | |||
| 2966 | 5 | $this->scheduledForSynchronization[$rootClassName][spl_object_hash($entity)] = $entity;  | 
            |
| 2967 | 5 | }  | 
            |
| 2968 | |||
| 2969 | /**  | 
            ||
| 2970 | * Checks whether the UnitOfWork has any pending insertions.  | 
            ||
| 2971 | *  | 
            ||
| 2972 | * @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise.  | 
            ||
| 2973 | */  | 
            ||
| 2974 | public function hasPendingInsertions()  | 
            ||
| 2975 |     { | 
            ||
| 2976 | return ! empty($this->entityInsertions);  | 
            ||
| 2977 | }  | 
            ||
| 2978 | |||
| 2979 | /**  | 
            ||
| 2980 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the  | 
            ||
| 2981 | * number of entities in the identity map.  | 
            ||
| 2982 | *  | 
            ||
| 2983 | * @return integer  | 
            ||
| 2984 | */  | 
            ||
| 2985 | 1 | public function size()  | 
            |
| 2986 |     { | 
            ||
| 2987 | 1 |         $countArray = array_map('count', $this->identityMap); | 
            |
| 2988 | |||
| 2989 | 1 | return array_sum($countArray);  | 
            |
| 2990 | }  | 
            ||
| 2991 | |||
| 2992 | /**  | 
            ||
| 2993 | * Gets the EntityPersister for an Entity.  | 
            ||
| 2994 | *  | 
            ||
| 2995 | * @param string $entityName The name of the Entity.  | 
            ||
| 2996 | *  | 
            ||
| 2997 | * @return \Doctrine\ORM\Persisters\Entity\EntityPersister  | 
            ||
| 2998 | */  | 
            ||
| 2999 | 1098 | public function getEntityPersister($entityName)  | 
            |
| 3000 |     { | 
            ||
| 3001 | 1098 |         if (isset($this->persisters[$entityName])) { | 
            |
| 3002 | 864 | return $this->persisters[$entityName];  | 
            |
| 3003 | }  | 
            ||
| 3004 | |||
| 3005 | 1098 | $class = $this->em->getClassMetadata($entityName);  | 
            |
| 3006 | |||
| 3007 |         switch (true) { | 
            ||
| 3008 | 1098 | case ($class->isInheritanceTypeNone()):  | 
            |
| 3009 | 1059 | $persister = new BasicEntityPersister($this->em, $class);  | 
            |
| 3010 | 1059 | break;  | 
            |
| 3011 | |||
| 3012 | 372 | case ($class->isInheritanceTypeSingleTable()):  | 
            |
| 3013 | 225 | $persister = new SingleTablePersister($this->em, $class);  | 
            |
| 3014 | 225 | break;  | 
            |
| 3015 | |||
| 3016 | 340 | case ($class->isInheritanceTypeJoined()):  | 
            |
| 3017 | 340 | $persister = new JoinedSubclassPersister($this->em, $class);  | 
            |
| 3018 | 340 | break;  | 
            |
| 3019 | |||
| 3020 | default:  | 
            ||
| 3021 |                 throw new \RuntimeException('No persister found for entity.'); | 
            ||
| 3022 | }  | 
            ||
| 3023 | |||
| 3024 | 1098 |         if ($this->hasCache && $class->cache !== null) { | 
            |
| 3025 | 124 | $persister = $this->em->getConfiguration()  | 
            |
| 3026 | 124 | ->getSecondLevelCacheConfiguration()  | 
            |
| 3027 | 124 | ->getCacheFactory()  | 
            |
| 3028 | 124 | ->buildCachedEntityPersister($this->em, $persister, $class);  | 
            |
| 3029 | }  | 
            ||
| 3030 | |||
| 3031 | 1098 | $this->persisters[$entityName] = $persister;  | 
            |
| 3032 | |||
| 3033 | 1098 | return $this->persisters[$entityName];  | 
            |
| 3034 | }  | 
            ||
| 3035 | |||
| 3036 | /**  | 
            ||
| 3037 | * Gets a collection persister for a collection-valued association.  | 
            ||
| 3038 | *  | 
            ||
| 3039 | * @param array $association  | 
            ||
| 3040 | *  | 
            ||
| 3041 | * @return \Doctrine\ORM\Persisters\Collection\CollectionPersister  | 
            ||
| 3042 | */  | 
            ||
| 3043 | 573 | public function getCollectionPersister(array $association)  | 
            |
| 3044 |     { | 
            ||
| 3045 | 573 | $role = isset($association['cache'])  | 
            |
| 3046 | 78 | ? $association['sourceEntity'] . '::' . $association['fieldName']  | 
            |
| 3047 | 573 | : $association['type'];  | 
            |
| 3048 | |||
| 3049 | 573 |         if (isset($this->collectionPersisters[$role])) { | 
            |
| 3050 | 451 | return $this->collectionPersisters[$role];  | 
            |
| 3051 | }  | 
            ||
| 3052 | |||
| 3053 | 573 | $persister = ClassMetadata::ONE_TO_MANY === $association['type']  | 
            |
| 3054 | 407 | ? new OneToManyPersister($this->em)  | 
            |
| 3055 | 573 | : new ManyToManyPersister($this->em);  | 
            |
| 3056 | |||
| 3057 | 573 |         if ($this->hasCache && isset($association['cache'])) { | 
            |
| 3058 | 77 | $persister = $this->em->getConfiguration()  | 
            |
| 3059 | 77 | ->getSecondLevelCacheConfiguration()  | 
            |
| 3060 | 77 | ->getCacheFactory()  | 
            |
| 3061 | 77 | ->buildCachedCollectionPersister($this->em, $persister, $association);  | 
            |
| 3062 | }  | 
            ||
| 3063 | |||
| 3064 | 573 | $this->collectionPersisters[$role] = $persister;  | 
            |
| 3065 | |||
| 3066 | 573 | return $this->collectionPersisters[$role];  | 
            |
| 3067 | }  | 
            ||
| 3068 | |||
| 3069 | /**  | 
            ||
| 3070 | * INTERNAL:  | 
            ||
| 3071 | * Registers an entity as managed.  | 
            ||
| 3072 | *  | 
            ||
| 3073 | * @param object $entity The entity.  | 
            ||
| 3074 | * @param array $id The identifier values.  | 
            ||
| 3075 | * @param array $data The original entity data.  | 
            ||
| 3076 | *  | 
            ||
| 3077 | * @return void  | 
            ||
| 3078 | */  | 
            ||
| 3079 | 209 | public function registerManaged($entity, array $id, array $data)  | 
            |
| 3080 |     { | 
            ||
| 3081 | 209 | $oid = spl_object_hash($entity);  | 
            |
| 3082 | |||
| 3083 | 209 | $this->entityIdentifiers[$oid] = $id;  | 
            |
| 3084 | 209 | $this->entityStates[$oid] = self::STATE_MANAGED;  | 
            |
| 3085 | 209 | $this->originalEntityData[$oid] = $data;  | 
            |
| 3086 | |||
| 3087 | 209 | $this->addToIdentityMap($entity);  | 
            |
| 3088 | |||
| 3089 | 203 |         if ($entity instanceof NotifyPropertyChanged && ( ! $entity instanceof Proxy || $entity->__isInitialized())) { | 
            |
| 3090 | 2 | $entity->addPropertyChangedListener($this);  | 
            |
| 3091 | }  | 
            ||
| 3092 | 203 | }  | 
            |
| 3093 | |||
| 3094 | /**  | 
            ||
| 3095 | * INTERNAL:  | 
            ||
| 3096 | * Clears the property changeset of the entity with the given OID.  | 
            ||
| 3097 | *  | 
            ||
| 3098 | * @param string $oid The entity's OID.  | 
            ||
| 3099 | *  | 
            ||
| 3100 | * @return void  | 
            ||
| 3101 | */  | 
            ||
| 3102 | public function clearEntityChangeSet($oid)  | 
            ||
| 3103 |     { | 
            ||
| 3104 | $this->entityChangeSets[$oid] = [];  | 
            ||
| 3105 | }  | 
            ||
| 3106 | |||
| 3107 | /* PropertyChangedListener implementation */  | 
            ||
| 3108 | |||
| 3109 | /**  | 
            ||
| 3110 | * Notifies this UnitOfWork of a property change in an entity.  | 
            ||
| 3111 | *  | 
            ||
| 3112 | * @param object $entity The entity that owns the property.  | 
            ||
| 3113 | * @param string $propertyName The name of the property that changed.  | 
            ||
| 3114 | * @param mixed $oldValue The old value of the property.  | 
            ||
| 3115 | * @param mixed $newValue The new value of the property.  | 
            ||
| 3116 | *  | 
            ||
| 3117 | * @return void  | 
            ||
| 3118 | */  | 
            ||
| 3119 | 3 | public function propertyChanged($entity, $propertyName, $oldValue, $newValue)  | 
            |
| 3120 |     { | 
            ||
| 3121 | 3 | $oid = spl_object_hash($entity);  | 
            |
| 3122 | 3 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 3123 | |||
| 3124 | 3 | $isAssocField = isset($class->associationMappings[$propertyName]);  | 
            |
| 3125 | |||
| 3126 | 3 |         if ( ! $isAssocField && ! isset($class->fieldMappings[$propertyName])) { | 
            |
| 3127 | 1 | return; // ignore non-persistent fields  | 
            |
| 3128 | }  | 
            ||
| 3129 | |||
| 3130 | // Update changeset and mark entity for synchronization  | 
            ||
| 3131 | 3 | $this->entityChangeSets[$oid][$propertyName] = [$oldValue, $newValue];  | 
            |
| 3132 | |||
| 3133 | 3 |         if ( ! isset($this->scheduledForSynchronization[$class->rootEntityName][$oid])) { | 
            |
| 3134 | 3 | $this->scheduleForDirtyCheck($entity);  | 
            |
| 3135 | }  | 
            ||
| 3136 | 3 | }  | 
            |
| 3137 | |||
| 3138 | /**  | 
            ||
| 3139 | * Gets the currently scheduled entity insertions in this UnitOfWork.  | 
            ||
| 3140 | *  | 
            ||
| 3141 | * @return array  | 
            ||
| 3142 | */  | 
            ||
| 3143 | 2 | public function getScheduledEntityInsertions()  | 
            |
| 3144 |     { | 
            ||
| 3145 | 2 | return $this->entityInsertions;  | 
            |
| 3146 | }  | 
            ||
| 3147 | |||
| 3148 | /**  | 
            ||
| 3149 | * Gets the currently scheduled entity updates in this UnitOfWork.  | 
            ||
| 3150 | *  | 
            ||
| 3151 | * @return array  | 
            ||
| 3152 | */  | 
            ||
| 3153 | 3 | public function getScheduledEntityUpdates()  | 
            |
| 3154 |     { | 
            ||
| 3155 | 3 | return $this->entityUpdates;  | 
            |
| 3156 | }  | 
            ||
| 3157 | |||
| 3158 | /**  | 
            ||
| 3159 | * Gets the currently scheduled entity deletions in this UnitOfWork.  | 
            ||
| 3160 | *  | 
            ||
| 3161 | * @return array  | 
            ||
| 3162 | */  | 
            ||
| 3163 | 1 | public function getScheduledEntityDeletions()  | 
            |
| 3164 |     { | 
            ||
| 3165 | 1 | return $this->entityDeletions;  | 
            |
| 3166 | }  | 
            ||
| 3167 | |||
| 3168 | /**  | 
            ||
| 3169 | * Gets the currently scheduled complete collection deletions  | 
            ||
| 3170 | *  | 
            ||
| 3171 | * @return array  | 
            ||
| 3172 | */  | 
            ||
| 3173 | 1 | public function getScheduledCollectionDeletions()  | 
            |
| 3174 |     { | 
            ||
| 3175 | 1 | return $this->collectionDeletions;  | 
            |
| 3176 | }  | 
            ||
| 3177 | |||
| 3178 | /**  | 
            ||
| 3179 | * Gets the currently scheduled collection inserts, updates and deletes.  | 
            ||
| 3180 | *  | 
            ||
| 3181 | * @return array  | 
            ||
| 3182 | */  | 
            ||
| 3183 | public function getScheduledCollectionUpdates()  | 
            ||
| 3184 |     { | 
            ||
| 3185 | return $this->collectionUpdates;  | 
            ||
| 3186 | }  | 
            ||
| 3187 | |||
| 3188 | /**  | 
            ||
| 3189 | * Helper method to initialize a lazy loading proxy or persistent collection.  | 
            ||
| 3190 | *  | 
            ||
| 3191 | * @param object $obj  | 
            ||
| 3192 | *  | 
            ||
| 3193 | * @return void  | 
            ||
| 3194 | */  | 
            ||
| 3195 | 2 | public function initializeObject($obj)  | 
            |
| 3196 |     { | 
            ||
| 3197 | 2 |         if ($obj instanceof Proxy) { | 
            |
| 3198 | 1 | $obj->__load();  | 
            |
| 3199 | |||
| 3200 | 1 | return;  | 
            |
| 3201 | }  | 
            ||
| 3202 | |||
| 3203 | 1 |         if ($obj instanceof PersistentCollection) { | 
            |
| 3204 | 1 | $obj->initialize();  | 
            |
| 3205 | }  | 
            ||
| 3206 | 1 | }  | 
            |
| 3207 | |||
| 3208 | /**  | 
            ||
| 3209 | * Helper method to show an object as string.  | 
            ||
| 3210 | *  | 
            ||
| 3211 | * @param object $obj  | 
            ||
| 3212 | *  | 
            ||
| 3213 | * @return string  | 
            ||
| 3214 | */  | 
            ||
| 3215 | 1 | private static function objToStr($obj)  | 
            |
| 3216 |     { | 
            ||
| 3217 | 1 | return method_exists($obj, '__toString') ? (string) $obj : get_class($obj).'@'.spl_object_hash($obj);  | 
            |
| 3218 | }  | 
            ||
| 3219 | |||
| 3220 | /**  | 
            ||
| 3221 | * Marks an entity as read-only so that it will not be considered for updates during UnitOfWork#commit().  | 
            ||
| 3222 | *  | 
            ||
| 3223 | * This operation cannot be undone as some parts of the UnitOfWork now keep gathering information  | 
            ||
| 3224 | * on this object that might be necessary to perform a correct update.  | 
            ||
| 3225 | *  | 
            ||
| 3226 | * @param object $object  | 
            ||
| 3227 | *  | 
            ||
| 3228 | * @return void  | 
            ||
| 3229 | *  | 
            ||
| 3230 | * @throws ORMInvalidArgumentException  | 
            ||
| 3231 | */  | 
            ||
| 3232 | 6 | public function markReadOnly($object)  | 
            |
| 3233 |     { | 
            ||
| 3234 | 6 |         if ( ! is_object($object) || ! $this->isInIdentityMap($object)) { | 
            |
| 3235 | 1 | throw ORMInvalidArgumentException::readOnlyRequiresManagedEntity($object);  | 
            |
| 3236 | }  | 
            ||
| 3237 | |||
| 3238 | 5 | $this->readOnlyObjects[spl_object_hash($object)] = true;  | 
            |
| 3239 | 5 | }  | 
            |
| 3240 | |||
| 3241 | /**  | 
            ||
| 3242 | * Is this entity read only?  | 
            ||
| 3243 | *  | 
            ||
| 3244 | * @param object $object  | 
            ||
| 3245 | *  | 
            ||
| 3246 | * @return bool  | 
            ||
| 3247 | *  | 
            ||
| 3248 | * @throws ORMInvalidArgumentException  | 
            ||
| 3249 | */  | 
            ||
| 3250 | 3 | public function isReadOnly($object)  | 
            |
| 3251 |     { | 
            ||
| 3252 | 3 |         if ( ! is_object($object)) { | 
            |
| 3253 | throw ORMInvalidArgumentException::readOnlyRequiresManagedEntity($object);  | 
            ||
| 3254 | }  | 
            ||
| 3255 | |||
| 3256 | 3 | return isset($this->readOnlyObjects[spl_object_hash($object)]);  | 
            |
| 3257 | }  | 
            ||
| 3258 | |||
| 3259 | /**  | 
            ||
| 3260 | * Perform whatever processing is encapsulated here after completion of the transaction.  | 
            ||
| 3261 | */  | 
            ||
| 3262 | 1025 | private function afterTransactionComplete()  | 
            |
| 3263 |     { | 
            ||
| 3264 |         $this->performCallbackOnCachedPersister(function (CachedPersister $persister) { | 
            ||
| 3265 | 93 | $persister->afterTransactionComplete();  | 
            |
| 3266 | 1025 | });  | 
            |
| 3267 | 1025 | }  | 
            |
| 3268 | |||
| 3269 | /**  | 
            ||
| 3270 | * Perform whatever processing is encapsulated here after completion of the rolled-back.  | 
            ||
| 3271 | */  | 
            ||
| 3272 | private function afterTransactionRolledBack()  | 
            ||
| 3273 |     { | 
            ||
| 3274 | 11 |         $this->performCallbackOnCachedPersister(function (CachedPersister $persister) { | 
            |
| 3275 | 3 | $persister->afterTransactionRolledBack();  | 
            |
| 3276 | 11 | });  | 
            |
| 3277 | 11 | }  | 
            |
| 3278 | |||
| 3279 | /**  | 
            ||
| 3280 | * Performs an action after the transaction.  | 
            ||
| 3281 | *  | 
            ||
| 3282 | * @param callable $callback  | 
            ||
| 3283 | */  | 
            ||
| 3284 | 1030 | private function performCallbackOnCachedPersister(callable $callback)  | 
            |
| 3285 |     { | 
            ||
| 3286 | 1030 |         if ( ! $this->hasCache) { | 
            |
| 3287 | 937 | return;  | 
            |
| 3288 | }  | 
            ||
| 3289 | |||
| 3290 | 93 |         foreach (array_merge($this->persisters, $this->collectionPersisters) as $persister) { | 
            |
| 3291 | 93 |             if ($persister instanceof CachedPersister) { | 
            |
| 3292 | 93 | $callback($persister);  | 
            |
| 3293 | }  | 
            ||
| 3294 | }  | 
            ||
| 3295 | 93 | }  | 
            |
| 3296 | |||
| 3297 | 1034 | private function dispatchOnFlushEvent()  | 
            |
| 3298 |     { | 
            ||
| 3299 | 1034 |         if ($this->evm->hasListeners(Events::onFlush)) { | 
            |
| 3300 | 4 | $this->evm->dispatchEvent(Events::onFlush, new OnFlushEventArgs($this->em));  | 
            |
| 3301 | }  | 
            ||
| 3302 | 1034 | }  | 
            |
| 3303 | |||
| 3304 | 1029 | private function dispatchPostFlushEvent()  | 
            |
| 3305 |     { | 
            ||
| 3306 | 1029 |         if ($this->evm->hasListeners(Events::postFlush)) { | 
            |
| 3307 | 5 | $this->evm->dispatchEvent(Events::postFlush, new PostFlushEventArgs($this->em));  | 
            |
| 3308 | }  | 
            ||
| 3309 | 1028 | }  | 
            |
| 3310 | |||
| 3311 | /**  | 
            ||
| 3312 | * Verifies if two given entities actually are the same based on identifier comparison  | 
            ||
| 3313 | *  | 
            ||
| 3314 | * @param object $entity1  | 
            ||
| 3315 | * @param object $entity2  | 
            ||
| 3316 | *  | 
            ||
| 3317 | * @return bool  | 
            ||
| 3318 | */  | 
            ||
| 3319 | 14 | private function isIdentifierEquals($entity1, $entity2)  | 
            |
| 3343 | |||
| 3344 | /**  | 
            ||
| 3345 | * @param array $identifier  | 
            ||
| 3346 | *  | 
            ||
| 3347 | * @return string  | 
            ||
| 3348 | */  | 
            ||
| 3349 | 1187 | private function hashIdentifier(array $identifier)  | 
            |
| 3350 |     { | 
            ||
| 3351 | 1187 |         return serialize(array_values(array_map('strval', $identifier))); | 
            |
| 3352 | }  | 
            ||
| 3353 | |||
| 3354 | /**  | 
            ||
| 3355 | * @param object $entity  | 
            ||
| 3356 | * @param object $managedCopy  | 
            ||
| 3357 | *  | 
            ||
| 3358 | * @throws ORMException  | 
            ||
| 3359 | * @throws OptimisticLockException  | 
            ||
| 3360 | * @throws TransactionRequiredException  | 
            ||
| 3361 | */  | 
            ||
| 3362 | 40 | private function mergeEntityStateIntoManagedCopy($entity, $managedCopy)  | 
            |
| 3363 |     { | 
            ||
| 3364 | 40 |         if (! $this->isLoaded($entity)) { | 
            |
| 3365 | 7 | return;  | 
            |
| 3366 | }  | 
            ||
| 3367 | |||
| 3368 | 33 |         if (! $this->isLoaded($managedCopy)) { | 
            |
| 3369 | 4 | $managedCopy->__load();  | 
            |
| 3370 | }  | 
            ||
| 3371 | |||
| 3372 | 33 | $class = $this->em->getClassMetadata(get_class($entity));  | 
            |
| 3373 | |||
| 3374 | 33 |         foreach ($this->reflectionPropertiesGetter->getProperties($class->name) as $prop) { | 
            |
| 3375 | 33 | $name = $prop->name;  | 
            |
| 3376 | |||
| 3377 | 33 | $prop->setAccessible(true);  | 
            |
| 3378 | |||
| 3379 | 33 |             if ( ! isset($class->associationMappings[$name])) { | 
            |
| 3380 | 33 |                 if ( ! $class->isIdentifier($name)) { | 
            |
| 3381 | 33 | $prop->setValue($managedCopy, $prop->getValue($entity));  | 
            |
| 3382 | }  | 
            ||
| 3383 |             } else { | 
            ||
| 3384 | 29 | $assoc2 = $class->associationMappings[$name];  | 
            |
| 3385 | |||
| 3386 | 29 |                 if ($assoc2['type'] & ClassMetadata::TO_ONE) { | 
            |
| 3387 | 25 | $other = $prop->getValue($entity);  | 
            |
| 3388 | 25 |                     if ($other === null) { | 
            |
| 3389 | 12 | $prop->setValue($managedCopy, null);  | 
            |
| 3390 |                     } else { | 
            ||
| 3391 | 16 |                         if ($other instanceof Proxy && !$other->__isInitialized()) { | 
            |
| 3392 | // do not merge fields marked lazy that have not been fetched.  | 
            ||
| 3393 | 4 | continue;  | 
            |
| 3394 | }  | 
            ||
| 3395 | |||
| 3396 | 12 |                         if ( ! $assoc2['isCascadeMerge']) { | 
            |
| 3397 | 6 |                             if ($this->getEntityState($other) === self::STATE_DETACHED) { | 
            |
| 3398 | 3 | $targetClass = $this->em->getClassMetadata($assoc2['targetEntity']);  | 
            |
| 3399 | 3 | $relatedId = $targetClass->getIdentifierValues($other);  | 
            |
| 3400 | |||
| 3401 | 3 |                                 if ($targetClass->subClasses) { | 
            |
| 3402 | 2 | $other = $this->em->find($targetClass->name, $relatedId);  | 
            |
| 3403 |                                 } else { | 
            ||
| 3404 | 1 | $other = $this->em->getProxyFactory()->getProxy(  | 
            |
| 3405 | 1 | $assoc2['targetEntity'],  | 
            |
| 3406 | 1 | $relatedId  | 
            |
| 3407 | );  | 
            ||
| 3408 | 1 | $this->registerManaged($other, $relatedId, []);  | 
            |
| 3409 | }  | 
            ||
| 3410 | }  | 
            ||
| 3411 | |||
| 3412 | 21 | $prop->setValue($managedCopy, $other);  | 
            |
| 3413 | }  | 
            ||
| 3414 | }  | 
            ||
| 3415 |                 } else { | 
            ||
| 3416 | 17 | $mergeCol = $prop->getValue($entity);  | 
            |
| 3417 | |||
| 3418 | 17 |                     if ($mergeCol instanceof PersistentCollection && ! $mergeCol->isInitialized()) { | 
            |
| 3419 | // do not merge fields marked lazy that have not been fetched.  | 
            ||
| 3420 | // keep the lazy persistent collection of the managed copy.  | 
            ||
| 3421 | 5 | continue;  | 
            |
| 3422 | }  | 
            ||
| 3423 | |||
| 3424 | 14 | $managedCol = $prop->getValue($managedCopy);  | 
            |
| 3425 | |||
| 3426 | 14 |                     if ( ! $managedCol) { | 
            |
| 3427 | 4 | $managedCol = new PersistentCollection(  | 
            |
| 3428 | 4 | $this->em,  | 
            |
| 3429 | 4 | $this->em->getClassMetadata($assoc2['targetEntity']),  | 
            |
| 3430 | 4 | new ArrayCollection  | 
            |
| 3431 | );  | 
            ||
| 3432 | 4 | $managedCol->setOwner($managedCopy, $assoc2);  | 
            |
| 3461 | |||
| 3462 | /**  | 
            ||
| 3463 | * This method called by hydrators, and indicates that hydrator totally completed current hydration cycle.  | 
            ||
| 3464 | * Unit of work able to fire deferred events, related to loading events here.  | 
            ||
| 3465 | *  | 
            ||
| 3466 | * @internal should be called internally from object hydrators  | 
            ||
| 3467 | */  | 
            ||
| 3468 | 911 | public function hydrationComplete()  | 
            |
| 3472 | |||
| 3473 | /**  | 
            ||
| 3474 | * @param string $entityName  | 
            ||
| 3475 | */  | 
            ||
| 3476 | 4 | private function clearIdentityMapForEntityName($entityName)  | 
            |
| 3488 | |||
| 3489 | /**  | 
            ||
| 3490 | * @param string $entityName  | 
            ||
| 3491 | */  | 
            ||
| 3492 | 4 | private function clearEntityInsertionsForEntityName($entityName)  | 
            |
| 3501 | |||
| 3502 | /**  | 
            ||
| 3503 | * @param ClassMetadata $class  | 
            ||
| 3504 | * @param mixed $identifierValue  | 
            ||
| 3505 | *  | 
            ||
| 3506 | * @return mixed the identifier after type conversion  | 
            ||
| 3507 | *  | 
            ||
| 3508 | * @throws \Doctrine\ORM\Mapping\MappingException if the entity has more than a single identifier  | 
            ||
| 3509 | */  | 
            ||
| 3510 | 937 | private function convertSingleFieldIdentifierToPHPValue(ClassMetadata $class, $identifierValue)  | 
            |
| 3517 | }  | 
            ||
| 3518 | 
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.