diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 1d36718b9..4744a005d 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -1851,7 +1851,7 @@ class UnitOfWork implements PropertyChangedListener // 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) { + if (($otherState = $this->getEntityState($other)) !== self::STATE_MANAGED && $otherState !== self::STATE_NEW) { $targetClass = $this->em->getClassMetadata($assoc2['targetEntity']); $relatedId = $targetClass->getIdentifierValues($other); diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2409Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2409Test.php new file mode 100644 index 000000000..c8b85f24f --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2409Test.php @@ -0,0 +1,72 @@ +useModelSet('cms'); + parent::setUp(); + } + + public function testIssue() + { + $em = $this->_em; + $uow = $em->getUnitOfWork(); + + $originalArticle = new CmsArticle(); + $originalUser = new CmsUser(); + + $originalArticle->topic = 'Unit Test'; + $originalArticle->text = 'How to write a test'; + + $originalUser->name = 'Doctrine Bot'; + $originalUser->username = 'DoctrineBot'; + $originalUser->status = 'active'; + + $originalUser->addArticle($originalArticle); + + $em->persist($originalUser); + $em->persist($originalArticle); + $em->flush(); + $em->clear(); + + $article = $em->find('Doctrine\Tests\Models\CMS\CmsArticle', $originalArticle->id); + $user = new CmsUser(); + + $user->name = 'Doctrine Bot 2.0'; + $user->username = 'BotDoctrine2'; + $user->status = 'new'; + + $article->setAuthor($user); + + $this->assertEquals(UnitOfWork::STATE_DETACHED, $uow->getEntityState($originalArticle)); + $this->assertEquals(UnitOfWork::STATE_DETACHED, $uow->getEntityState($originalUser)); + $this->assertEquals(UnitOfWork::STATE_MANAGED, $uow->getEntityState($article)); + $this->assertEquals(UnitOfWork::STATE_NEW, $uow->getEntityState($user)); + + $em->detach($user); + $em->detach($article); + + $userMerged = $em->merge($user); + $articleMerged = $em->merge($article); + + $this->assertEquals(UnitOfWork::STATE_NEW, $uow->getEntityState($user)); + $this->assertEquals(UnitOfWork::STATE_DETACHED, $uow->getEntityState($article)); + $this->assertEquals(UnitOfWork::STATE_MANAGED, $uow->getEntityState($userMerged)); + $this->assertEquals(UnitOfWork::STATE_MANAGED, $uow->getEntityState($articleMerged)); + + $this->assertNotSame($user, $userMerged); + $this->assertNotSame($article, $articleMerged); + $this->assertNotSame($userMerged, $articleMerged->user); + $this->assertSame($user, $articleMerged->user); + } +} \ No newline at end of file