From 7807d6806c969113ff41a028ffa2a306fc889f63 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Mon, 11 Jun 2012 15:43:10 -0300 Subject: [PATCH] fix delete join table not owning side --- .../ORM/Persisters/BasicEntityPersister.php | 23 ++++++++++--------- .../ORM/Functional/Ticket/DDC1843Test.php | 22 +++++++++++------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 021c8692e..8ca2c299b 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -420,11 +420,16 @@ class BasicEntityPersister // @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['targetEntity'] == $mapping['sourceEntity']); + $otherKeys = array(); + $keys = array(); if ( ! $mapping['isOwningSide']) { - $relatedClass = $this->_em->getClassMetadata($mapping['targetEntity']); - $mapping = $relatedClass->associationMappings[$mapping['mappedBy']]; - $keys = array_keys($mapping['relationToTargetKeyColumns']); + $relatedClass = $this->_em->getClassMetadata($mapping['targetEntity']); + $mapping = $relatedClass->associationMappings[$mapping['mappedBy']]; + + foreach ($mapping['joinTable']['inverseJoinColumns'] as $joinColumn) { + $keys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $relatedClass); + } if ($selfReferential) { $otherKeys = array_keys($mapping['relationToSourceKeyColumns']); @@ -438,16 +443,12 @@ class BasicEntityPersister } if ( ! isset($mapping['isOnDeleteCascade'])) { - $this->_conn->delete( - $this->quoteStrategy->getJoinTableName($mapping, $this->_class), - array_combine($keys, $identifier) - ); + + $joinTableName = $this->quoteStrategy->getJoinTableName($mapping, $this->_class); + $this->_conn->delete($joinTableName, array_combine($keys, $identifier)); if ($selfReferential) { - $this->_conn->delete( - $this->quoteStrategy->getJoinTableName($mapping, $this->_class), - array_combine($otherKeys, $identifier) - ); + $this->_conn->delete($joinTableName,array_combine($otherKeys, $identifier)); } } } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1843Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1843Test.php index 9ee846cbd..9f6a17ab4 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1843Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1843Test.php @@ -22,10 +22,10 @@ class DDC1843Test extends \Doctrine\Tests\OrmFunctionalTestCase try { $this->_schemaTool->createSchema(array( - //$this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\User'), + $this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\User'), $this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\Group'), - //$this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\Phone'), - //$this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\Address'), + $this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\Phone'), + $this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\Address'), )); } catch(\Exception $e) { $this->fail($e->getMessage()); @@ -110,14 +110,14 @@ class DDC1843Test extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals('Bar 33', $e3->name); $this->assertEquals('Foo 44', $e4->name); - $this->markTestIncomplete(); - // Delete - $this->_em->remove($e1); - $this->_em->remove($e2); - $this->_em->remove($e3); $this->_em->remove($e4); + $this->_em->remove($e3); + $this->_em->remove($e2); + $this->_em->remove($e1); + $this->_em->flush(); + $this->_em->clear(); $this->assertInstanceOf(self::CLASS_NAME, $e1); @@ -125,6 +125,12 @@ class DDC1843Test extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertInstanceOf(self::CLASS_NAME, $e3); $this->assertInstanceOf(self::CLASS_NAME, $e4); + // Retreave + $e1 = $this->_em->find(self::CLASS_NAME, $e1Id); + $e2 = $this->_em->find(self::CLASS_NAME, $e2Id); + $e3 = $this->_em->find(self::CLASS_NAME, $e3Id); + $e4 = $this->_em->find(self::CLASS_NAME, $e4Id); + $this->assertNull($e1); $this->assertNull($e2); $this->assertNull($e3);