From 9d471de22d2806f758aa11c76e6fe4b81ef16c05 Mon Sep 17 00:00:00 2001
From: Dave Keen <dev@ruffness.com>
Date: Wed, 22 Sep 2010 02:12:47 +0200
Subject: [PATCH 149/149] DDC-763

---
 lib/Doctrine/ORM/UnitOfWork.php |  207 +++++++++++++++++++++-----------------
 1 files changed, 114 insertions(+), 93 deletions(-)

diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php
index a051785..697e68e 100644
--- a/lib/Doctrine/ORM/UnitOfWork.php
+++ b/lib/Doctrine/ORM/UnitOfWork.php
@@ -215,7 +215,14 @@ class UnitOfWork implements PropertyChangedListener
      * @var array
      */
     private $orphanRemovals = array();
-    
+
+    /**
+     * Entities that have been already merged
+     *
+     * @var array
+     */
+    private $mergedEntities = array();
+
     //private $_readOnlyObjects = array();
 
     /**
@@ -1336,118 +1343,124 @@ class UnitOfWork implements PropertyChangedListener
     private function doMerge($entity, array &$visited, $prevManagedCopy = null, $assoc = null)
     {
         $oid = spl_object_hash($entity);
+        
         if (isset($visited[$oid])) {
             return; // Prevent infinite recursion
         }
-
+        
         $visited[$oid] = $entity; // mark visited
 
         $class = $this->em->getClassMetadata(get_class($entity));
 
-        // First we assume DETACHED, although it can still be NEW but we can avoid
-        // an extra db-roundtrip this way. If it is not MANAGED but has an identity,
-        // we need to fetch it from the db anyway in order to merge.
-        // MANAGED entities are ignored by the merge operation.
-        if ($this->getEntityState($entity, self::STATE_DETACHED) == self::STATE_MANAGED) {
-            $managedCopy = $entity;
+        // If we have already merged this entity then get the managed copy from the merged entities
+        if (isset($this->mergedEntities[$oid])) {
+            $managedCopy = $this->mergedEntities[$oid];
         } else {
-            // Try to look the entity up in the identity map.
-            $id = $class->getIdentifierValues($entity);
-
-            // If there is no ID, it is actually NEW.
-            if ( ! $id) {
-                $managedCopy = $class->newInstance();
-                $this->persistNew($class, $managedCopy);
+            // First we assume DETACHED, although it can still be NEW but we can avoid
+            // an extra db-roundtrip this way. If it is not MANAGED but has an identity,
+            // we need to fetch it from the db anyway in order to merge.
+            // MANAGED entities are ignored by the merge operation.
+            if ($this->getEntityState($entity, self::STATE_DETACHED) == self::STATE_MANAGED) {
+                $managedCopy = $entity;
             } else {
-                $managedCopy = $this->tryGetById($id, $class->rootEntityName);
-                if ($managedCopy) {
-                    // We have the entity in-memory already, just make sure its not removed.
-                    if ($this->getEntityState($managedCopy) == self::STATE_REMOVED) {
-                        throw new InvalidArgumentException('Removed entity detected during merge.'
-                                . ' Can not merge with a removed entity.');
-                    }
-                } else {
-                    // We need to fetch the managed copy in order to merge.
-                    $managedCopy = $this->em->find($class->name, $id);
-                }
+                // Try to look the entity up in the identity map.
+                $id = $class->getIdentifierValues($entity);
 
-                if ($managedCopy === null) {
-                    // If the identifier is ASSIGNED, it is NEW, otherwise an error
-                    // since the managed entity was not found.
-                    if ($class->isIdentifierNatural()) {
-                        $managedCopy = $class->newInstance();
-                        $class->setIdentifierValues($managedCopy, $id);
-                        $this->persistNew($class, $managedCopy);
+                // If there is no ID, it is actually NEW.
+                if ( ! $id ) {
+                    $managedCopy = $class->newInstance();
+                    $this->persistNew($class, $managedCopy);
+                } else {
+                    $managedCopy = $this->tryGetById($id, $class->rootEntityName);
+                    if ($managedCopy) {
+                        // We have the entity in-memory already, just make sure its not removed.
+                        if ($this->getEntityState($managedCopy) == self::STATE_REMOVED) {
+                            throw new InvalidArgumentException('Removed entity detected during merge.'
+                                    . ' Can not merge with a removed entity.');
+                        }
                     } else {
-                        throw new EntityNotFoundException;
+                        // We need to fetch the managed copy in order to merge.
+                        $managedCopy = $this->em->find($class->name, $id);
                     }
-                }
-            }
 
-            if ($class->isVersioned) {
-                $managedCopyVersion = $class->reflFields[$class->versionField]->getValue($managedCopy);
-                $entityVersion = $class->reflFields[$class->versionField]->getValue($entity);
-                // Throw exception if versions dont match.
-                if ($managedCopyVersion != $entityVersion) {
-                    throw OptimisticLockException::lockFailedVersionMissmatch($entityVersion, $managedCopyVersion);
+                    if ($managedCopy === null) {
+                        // If the identifier is ASSIGNED, it is NEW, otherwise an error
+                        // since the managed entity was not found.
+                        if ($class->isIdentifierNatural()) {
+                            $managedCopy = $class->newInstance();
+                            $class->setIdentifierValues($managedCopy, $id);
+                            $this->persistNew($class, $managedCopy);
+                        } else {
+                            throw new EntityNotFoundException;
+                        }
+                    }
                 }
-            }
 
-            // Merge state of $entity into existing (managed) entity
-            foreach ($class->reflFields as $name => $prop) {
-                if ( ! isset($class->associationMappings[$name])) {
-                    if ( ! $class->isIdentifier($name)) {
-                        $prop->setValue($managedCopy, $prop->getValue($entity));
+                if ($class->isVersioned) {
+                    $managedCopyVersion = $class->reflFields[$class->versionField]->getValue($managedCopy);
+                    $entityVersion = $class->reflFields[$class->versionField]->getValue($entity);
+                    // Throw exception if versions dont match.
+                    if ($managedCopyVersion != $entityVersion) {
+                        throw OptimisticLockException::lockFailedVersionMissmatch($entityVersion, $managedCopyVersion);
                     }
-                } else {
-                    $assoc2 = $class->associationMappings[$name];
-                    if ($assoc2['type'] & ClassMetadata::TO_ONE) {
-                        $other = $prop->getValue($entity);
-                        if ($other === null) {
-                            $prop->setValue($managedCopy, null);
-                        } else if ($other instanceof Proxy && !$other->__isInitialized__) {
-                            // do not merge fields marked lazy that have not been fetched.
-                            continue;
-                        } else if ( ! $assoc2['isCascadeMerge']) {
-                            if ($this->getEntityState($other, self::STATE_DETACHED) == self::STATE_MANAGED) {
-                                $prop->setValue($managedCopy, $other);
-                            } else {
-                                $targetClass = $this->em->getClassMetadata($assoc2['targetEntity']);
-                                $id = $targetClass->getIdentifierValues($other);
-                                $proxy = $this->em->getProxyFactory()->getProxy($assoc2['targetEntity'], $id);
-                                $prop->setValue($managedCopy, $proxy);
-                                $this->registerManaged($proxy, $id, array());
-                            }
+                }
+
+                // Merge state of $entity into existing (managed) entity
+                foreach ($class->reflFields as $name => $prop) {
+                    if ( ! isset($class->associationMappings[$name])) {
+                        if ( ! $class->isIdentifier($name)) {
+                            $prop->setValue($managedCopy, $prop->getValue($entity));
                         }
                     } else {
-                        $mergeCol = $prop->getValue($entity);
-                        if ($mergeCol instanceof PersistentCollection && !$mergeCol->isInitialized()) {
-                            // do not merge fields marked lazy that have not been fetched.
-                            // keep the lazy persistent collection of the managed copy.
-                            continue;
-                        }
+                        $assoc2 = $class->associationMappings[$name];
+                        if ($assoc2['type'] & ClassMetadata::TO_ONE) {
+                            $other = $prop->getValue($entity);
+                            if ($other === null) {
+                                $prop->setValue($managedCopy, null);
+                            } else if ($other instanceof Proxy && !$other->__isInitialized__) {
+                                // do not merge fields marked lazy that have not been fetched.
+                                continue;
+                            } else if ( ! $assoc2['isCascadeMerge']) {
+                                if ($this->getEntityState($other, self::STATE_DETACHED) == self::STATE_MANAGED) {
+                                    $prop->setValue($managedCopy, $other);
+                                } else {
+                                    $targetClass = $this->em->getClassMetadata($assoc2['targetEntity']);
+                                    $id = $targetClass->getIdentifierValues($other);
+                                    $proxy = $this->em->getProxyFactory()->getProxy($assoc2['targetEntity'], $id);
+                                    $prop->setValue($managedCopy, $proxy);
+                                    $this->registerManaged($proxy, $id, array());
+                                }
+                            }
+                        } else {
+                            $mergeCol = $prop->getValue($entity);
+                            if ($mergeCol instanceof PersistentCollection && !$mergeCol->isInitialized()) {
+                                // do not merge fields marked lazy that have not been fetched.
+                                // keep the lazy persistent collection of the managed copy.
+                                continue;
+                            }
 
-                        $managedCol = $prop->getValue($managedCopy);
-                        if (!$managedCol) {
-                            $managedCol = new PersistentCollection($this->em,
-                                    $this->em->getClassMetadata($assoc2['targetEntity']),
-                                    new ArrayCollection
-                                    );
-                            $managedCol->setOwner($managedCopy, $assoc2);
-                            $prop->setValue($managedCopy, $managedCol);
-                            $this->originalEntityData[$oid][$name] = $managedCol;
+                            $managedCol = $prop->getValue($managedCopy);
+                            if (!$managedCol) {
+                                $managedCol = new PersistentCollection($this->em,
+                                        $this->em->getClassMetadata($assoc2['targetEntity']),
+                                        new ArrayCollection
+                                        );
+                                $managedCol->setOwner($managedCopy, $assoc2);
+                                $prop->setValue($managedCopy, $managedCol);
+                                $this->originalEntityData[$oid][$name] = $managedCol;
+                            }
+                            $managedCol->setInitialized($assoc2['isCascadeMerge']);
                         }
-                        $managedCol->setInitialized($assoc2['isCascadeMerge']);
+                    }
+                    if ($class->isChangeTrackingNotify()) {
+                        // Just treat all properties as changed, there is no other choice.
+                        $this->propertyChanged($managedCopy, $name, null, $prop->getValue($managedCopy));
                     }
                 }
-                if ($class->isChangeTrackingNotify()) {
-                    // Just treat all properties as changed, there is no other choice.
-                    $this->propertyChanged($managedCopy, $name, null, $prop->getValue($managedCopy));
+                if ($class->isChangeTrackingDeferredExplicit()) {
+                    $this->scheduleForDirtyCheck($entity);
                 }
             }
-            if ($class->isChangeTrackingDeferredExplicit()) {
-                $this->scheduleForDirtyCheck($entity);
-            }
         }
 
         if ($prevManagedCopy !== null) {
@@ -1456,7 +1469,9 @@ class UnitOfWork implements PropertyChangedListener
             if ($assoc['type'] & ClassMetadata::TO_ONE) {
                 $prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy);
             } else {
-                $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->unwrap()->add($managedCopy);
+                // Only add the managed copy to the array collection if it isn't already there
+                $arrayCollection = $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->unwrap();
+                if (!$arrayCollection->contains($managedCopy)) $arrayCollection->add($managedCopy);
                 if ($assoc['type'] == ClassMetadata::ONE_TO_MANY) {
                     $class->reflFields[$assoc['mappedBy']]->setValue($managedCopy, $prevManagedCopy);
                 }
@@ -1468,6 +1483,9 @@ class UnitOfWork implements PropertyChangedListener
 
         $this->cascadeMerge($entity, $managedCopy, $visited);
 
+        // Mark the managed copy merged
+        $this->mergedEntities[$oid] = $managedCopy;
+        
         return $managedCopy;
     }
     
@@ -1635,8 +1653,10 @@ class UnitOfWork implements PropertyChangedListener
                     $this->doMerge($relatedEntity, $visited, $managedCopy, $assoc);
                 }
                 // Now we want to initialize $snapshot to whatever was originally in the database
-                $managedCollection = $class->reflFields[$assoc['fieldName']]->getValue($managedCopy);
-                $managedCollection->takeSnapshotFromDatabase();
+                if ($assoc['isOwningSide']) {
+                    $managedCollection = $class->reflFields[$assoc['fieldName']]->getValue($managedCopy);
+                    $managedCollection->takeSnapshotFromDatabase();
+                }
             } else if ($relatedEntities !== null) {
                 $this->doMerge($relatedEntities, $visited, $managedCopy, $assoc);
             }
@@ -1770,7 +1790,8 @@ class UnitOfWork implements PropertyChangedListener
         $this->collectionDeletions =
         $this->collectionUpdates =
         $this->extraUpdates =
-        $this->orphanRemovals = array();
+        $this->orphanRemovals =
+        $this->mergedEntities = array();
         if ($this->commitOrderCalculator !== null) {
             $this->commitOrderCalculator->clear();
         }
-- 
1.6.5.1.1367.gcd48

