diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 684210272..d62eb62c7 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -1456,7 +1456,8 @@ class UnitOfWork implements PropertyChangedListener } if ($assoc2['isCascadeMerge']) { $managedCol->initialize(); - if (!$managedCol->isEmpty()) { + // clear and set dirty a managed collection if its not also the same collection to merge from. + if (!$managedCol->isEmpty() && $managedCol != $mergeCol) { $managedCol->unwrap()->clear(); $managedCol->setDirty(true); if ($assoc2['isOwningSide'] && $assoc2['type'] == ClassMetadata::MANY_TO_MANY && $class->isChangeTrackingNotify()) { @@ -1655,6 +1656,10 @@ class UnitOfWork implements PropertyChangedListener } $relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity); if ($relatedEntities instanceof Collection) { + if ($relatedEntities === $class->reflFields[$assoc['fieldName']]->getValue($managedCopy)) { + continue; + } + if ($relatedEntities instanceof PersistentCollection) { // Unwrap so that foreach() does not initialize $relatedEntities = $relatedEntities->unwrap(); diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1276Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1276Test.php new file mode 100644 index 000000000..1b9dc8c99 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1276Test.php @@ -0,0 +1,50 @@ +useModelSet('cms'); + parent::setUp(); + } + + public function testIssue() + { + $user = new CmsUser(); + $user->name = "Benjamin"; + $user->username = "beberlei"; + $user->status = "active"; + $this->_em->persist($user); + + for ($i = 0; $i < 2; $i++) { + $group = new CmsGroup(); + $group->name = "group".$i; + $user->groups[] = $group; + $this->_em->persist($group); + } + $this->_em->flush(); + $this->_em->clear(); + + $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $user->id); + $cloned = clone $user; + + $this->assertSame($user->groups, $cloned->groups); + $this->assertEquals(2, count($user->groups)); + $this->_em->merge($cloned); + + $this->assertEquals(2, count($user->groups)); + + $this->_em->flush(); + } +} \ No newline at end of file