From 559303430ac922fab4b80064c3ac0b3e1a4160f4 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Thu, 14 Mar 2013 23:41:31 +0100 Subject: [PATCH] [DDC-1666] Fix bug where orphan removal on one-to-one associations lead to unique constraint errors when replacing an entity with a new one. --- .../ORM/Mapping/ClassMetadataInfo.php | 4 ++ .../ORM/Functional/Ticket/DDC1666Test.php | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1666Test.php diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index 411357af7..1e038d659 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1487,6 +1487,10 @@ class ClassMetadataInfo implements ClassMetadata $mapping['orphanRemoval'] = isset($mapping['orphanRemoval']) ? (bool) $mapping['orphanRemoval'] : false; $mapping['isCascadeRemove'] = $mapping['orphanRemoval'] ? true : $mapping['isCascadeRemove']; + if ($mapping['orphanRemoval']) { + unset($mapping['unique']); + } + if (isset($mapping['id']) && $mapping['id'] === true && !$mapping['isOwningSide']) { throw MappingException::illegalInverseIdentifierAssociation($this->name, $mapping['fieldName']); } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1666Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1666Test.php new file mode 100644 index 000000000..e664e537c --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1666Test.php @@ -0,0 +1,40 @@ +useModelSet('cms'); + parent::setUp(); + } + + public function testGivenOrphanRemovalOneToOne_WhenReplacing_ThenNoUniqueConstraintError() + { + $user = new CmsUser(); + $user->name = "Benjamin"; + $user->username = "beberlei"; + $user->status = "something"; + $user->setEmail($email = new CmsEmail()); + $email->setEmail("kontakt@beberlei.de"); + + $this->_em->persist($user); + $this->_em->flush(); + + $this->assertTrue($this->_em->contains($email)); + + $user->setEmail($newEmail = new CmsEmail()); + $newEmail->setEmail("benjamin.eberlei@googlemail.com"); + + $this->_em->flush(); + + $this->assertFalse($this->_em->contains($email)); + } +}