From ede62052045ac23e62b604f140743e9bab98a74d Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sat, 10 Jul 2010 13:35:58 +0200 Subject: [PATCH] DDC-130 - Make self-referential relationsships work by deleting both the owning and the inverse pair of keys --- .../ORM/Persisters/BasicEntityPersister.php | 14 ++++++++++++++ .../ORM/Functional/ClassTableInheritanceTest.php | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index dd5eee975..8fba4d000 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -345,16 +345,30 @@ class BasicEntityPersister foreach ($this->_class->associationMappings AS $mapping) { /* @var $mapping \Doctrine\ORM\Mapping\AssociationMapping */ if ($mapping->isManyToMany()) { + // @Todo this only covers scenarios with no inheritance or of the same level. Is there something + // like self-referential relationship between different levels of an inheritance hierachy? I hope not! + $selfReferential = ($mapping->targetEntityName == $mapping->sourceEntityName); + if (!$mapping->isOwningSide) { $relatedClass = $this->_em->getClassMetadata($mapping->targetEntityName); $mapping = $relatedClass->associationMappings[$mapping->mappedBy]; $keys = array_keys($mapping->relationToTargetKeyColumns); + if ($selfReferential) { + $otherKeys = array_keys($mapping->relationToSourceKeyColumns); + } } else { $keys = array_keys($mapping->relationToSourceKeyColumns); + if ($selfReferential) { + $otherKeys = array_keys($mapping->relationToTargetKeyColumns); + } } if(!$mapping->isOnDeleteCascade) { $this->_conn->delete($mapping->joinTable['name'], array_combine($keys, $identifier)); + + if ($selfReferential) { + $this->_conn->delete($mapping->joinTable['name'], array_combine($otherKeys, $identifier)); + } } } } diff --git a/tests/Doctrine/Tests/ORM/Functional/ClassTableInheritanceTest.php b/tests/Doctrine/Tests/ORM/Functional/ClassTableInheritanceTest.php index 78d5d7987..5ba6b2aa7 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ClassTableInheritanceTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ClassTableInheritanceTest.php @@ -300,7 +300,7 @@ class ClassTableInheritanceTest extends \Doctrine\Tests\OrmFunctionalTestCase */ public function testDeleteJoinTableRecords() { - $this->markTestSkipped('Nightmare! friends adds both ID 6-7 and 7-6 into two rows of the join table. How to detect this?'); + #$this->markTestSkipped('Nightmare! friends adds both ID 6-7 and 7-6 into two rows of the join table. How to detect this?'); $employee1 = new CompanyEmployee(); $employee1->setName('gblanco');