From b0e4d06c405f82af3524f9021cd095104040eefe Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sat, 10 Jul 2010 13:12:33 +0200 Subject: [PATCH] DDC-130 - Refactored deleteJoinTableRecords() even more, simplified approach disfavouring evil legacy database schemas --- .../ORM/Mapping/ManyToManyMapping.php | 22 +++++-------------- .../ORM/Persisters/BasicEntityPersister.php | 21 +++++++----------- .../Tests/ORM/Mapping/ClassMetadataTest.php | 6 ++--- 3 files changed, 16 insertions(+), 33 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/ManyToManyMapping.php b/lib/Doctrine/ORM/Mapping/ManyToManyMapping.php index 0a1aaa34e..a308b4eb8 100644 --- a/lib/Doctrine/ORM/Mapping/ManyToManyMapping.php +++ b/lib/Doctrine/ORM/Mapping/ManyToManyMapping.php @@ -69,18 +69,11 @@ class ManyToManyMapping extends AssociationMapping public $orderBy; /** - * READ-ONLY: Are entries on the owning side of this join-table deleted through a database onDelete="CASCADE" operation? + * READ-ONLY: Are entries on the owning AND inverse side of this join-table deleted through a database onDelete="CASCADE" operation? * * @var bool */ - public $owningIsOnDeleteCascade = false; - - /** - * READ-ONLY: Are entries on the inverse side of this join-table deleted through a database onDelete="CASCADE" operation? - * - * @var bool - */ - public $inverseIsOnDeleteCascade = false; + public $isOnDeleteCascade = false; /** * {@inheritdoc} @@ -130,7 +123,7 @@ class ManyToManyMapping extends AssociationMapping foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) { if (isset($joinColumn['onDelete']) && strtolower($joinColumn['onDelete']) == 'cascade') { - $this->owningIsOnDeleteCascade = true; + $this->isOnDeleteCascade = true; } $this->relationToSourceKeyColumns[$joinColumn['name']] = $joinColumn['referencedColumnName']; @@ -139,7 +132,7 @@ class ManyToManyMapping extends AssociationMapping foreach ($mapping['joinTable']['inverseJoinColumns'] as $inverseJoinColumn) { if (isset($inverseJoinColumn['onDelete']) && strtolower($inverseJoinColumn['onDelete']) == 'cascade') { - $this->inverseIsOnDeleteCascade = true; + $this->isOnDeleteCascade = true; } $this->relationToTargetKeyColumns[$inverseJoinColumn['name']] = $inverseJoinColumn['referencedColumnName']; @@ -191,11 +184,8 @@ class ManyToManyMapping extends AssociationMapping $serialized[] = 'joinTableColumns'; $serialized[] = 'relationToSourceKeyColumns'; $serialized[] = 'relationToTargetKeyColumns'; - if ($this->owningIsOnDeleteCascade) { - $serialized[] = 'owningIsOnDeleteCascade'; - } - if ($this->inverseIsOnDeleteCascade) { - $serialized[] = 'inverseIsOnDeleteCascade'; + if ($this->isOnDeleteCascade) { + $serialized[] = 'isOnDeleteCascade'; } if ($this->orderBy) { $serialized[] = 'orderBy'; diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index f86128fff..dd5eee975 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -345,21 +345,16 @@ class BasicEntityPersister foreach ($this->_class->associationMappings AS $mapping) { /* @var $mapping \Doctrine\ORM\Mapping\AssociationMapping */ if ($mapping->isManyToMany()) { - if($mapping->isOwningSide && (!$mapping->owningIsOnDeleteCascade || !$this->_platform->supportsForeignKeyConstraints())) { - $this->_conn->delete( - $mapping->joinTable['name'], - array_combine(array_keys($mapping->relationToSourceKeyColumns), $identifier) - ); - } else if (!$mapping->isOwningSide) { + if (!$mapping->isOwningSide) { $relatedClass = $this->_em->getClassMetadata($mapping->targetEntityName); - $relatedMapping = $relatedClass->associationMappings[$mapping->mappedBy]; + $mapping = $relatedClass->associationMappings[$mapping->mappedBy]; + $keys = array_keys($mapping->relationToTargetKeyColumns); + } else { + $keys = array_keys($mapping->relationToSourceKeyColumns); + } - if (!$relatedMapping->inverseIsOnDeleteCascade || !$this->_platform->supportsForeignKeyConstraints()) { - $this->_conn->delete( - $relatedMapping->joinTable['name'], - array_combine(array_keys($relatedMapping->relationToTargetKeyColumns), $identifier) - ); - } + if(!$mapping->isOnDeleteCascade) { + $this->_conn->delete($mapping->joinTable['name'], array_combine($keys, $identifier)); } } } diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php index 565826e9a..620838a6b 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php @@ -107,8 +107,7 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase 'joinColumns' => array(array('name' => 'CmsUser_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE')), 'inverseJoinColumns' => array(array('name' => 'CmsGroup_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE')) ), $assoc->joinTable); - $this->assertTrue($assoc->owningIsOnDeleteCascade); - $this->assertTrue($assoc->inverseIsOnDeleteCascade); + $this->assertTrue($assoc->isOnDeleteCascade); } public function testSerializeManyToManyJoinTableCascade() @@ -124,8 +123,7 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase $assoc = $cm->associationMappings['groups']; $assoc = unserialize(serialize($assoc)); - $this->assertTrue($assoc->owningIsOnDeleteCascade); - $this->assertTrue($assoc->inverseIsOnDeleteCascade); + $this->assertTrue($assoc->isOnDeleteCascade); } /**