From ffc47733b73528dd520189a1af12f12e7c05f1b4 Mon Sep 17 00:00:00 2001
From: Dave Keen <dev@ruffness.com>
Date: Wed, 22 Sep 2010 00:06:35 +0200
Subject: [PATCH 147/147] DDC-758

---
 lib/Doctrine/ORM/PersistentCollection.php |   11 +++++++++++
 lib/Doctrine/ORM/UnitOfWork.php           |    3 +++
 2 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/lib/Doctrine/ORM/PersistentCollection.php b/lib/Doctrine/ORM/PersistentCollection.php
index 6c09e96..4380d5e 100644
--- a/lib/Doctrine/ORM/PersistentCollection.php
+++ b/lib/Doctrine/ORM/PersistentCollection.php
@@ -217,6 +217,17 @@ final class PersistentCollection implements Collection
         }
     }
 
+    public function takeSnapshotFromDatabase()
+    {
+        $dummyCollection = new PersistentCollection($this->em, $this->typeClass, new \Doctrine\Common\Collections\ArrayCollection());
+        $dummyCollection->setOwner($this->owner, $this->association);
+        $this->em->getUnitOfWork()->loadCollection($dummyCollection);
+
+        $this->snapshot = $dummyCollection->coll->toArray();
+        
+        $this->setDirty(true);
+    }
+
     /**
      * INTERNAL:
      * Tells this collection to take a snapshot of its current state.
diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php
index e3cd015..a051785 100644
--- a/lib/Doctrine/ORM/UnitOfWork.php
+++ b/lib/Doctrine/ORM/UnitOfWork.php
@@ -1634,6 +1634,9 @@ class UnitOfWork implements PropertyChangedListener
                 foreach ($relatedEntities as $relatedEntity) {
                     $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();
             } else if ($relatedEntities !== null) {
                 $this->doMerge($relatedEntities, $visited, $managedCopy, $assoc);
             }
-- 
1.6.5.1.1367.gcd48

