1
0
Fork 0
mirror of synced 2025-04-03 13:23:37 +03:00

DDC-130 - Refactored deleteJoinTableRecords() even more, simplified approach disfavouring evil legacy database schemas

This commit is contained in:
Benjamin Eberlei 2010-07-10 13:12:33 +02:00
parent 1794127d51
commit b0e4d06c40
3 changed files with 16 additions and 33 deletions

View file

@ -69,18 +69,11 @@ class ManyToManyMapping extends AssociationMapping
public $orderBy; 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 * @var bool
*/ */
public $owningIsOnDeleteCascade = false; public $isOnDeleteCascade = 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;
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -130,7 +123,7 @@ class ManyToManyMapping extends AssociationMapping
foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) { foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) {
if (isset($joinColumn['onDelete']) && strtolower($joinColumn['onDelete']) == 'cascade') { if (isset($joinColumn['onDelete']) && strtolower($joinColumn['onDelete']) == 'cascade') {
$this->owningIsOnDeleteCascade = true; $this->isOnDeleteCascade = true;
} }
$this->relationToSourceKeyColumns[$joinColumn['name']] = $joinColumn['referencedColumnName']; $this->relationToSourceKeyColumns[$joinColumn['name']] = $joinColumn['referencedColumnName'];
@ -139,7 +132,7 @@ class ManyToManyMapping extends AssociationMapping
foreach ($mapping['joinTable']['inverseJoinColumns'] as $inverseJoinColumn) { foreach ($mapping['joinTable']['inverseJoinColumns'] as $inverseJoinColumn) {
if (isset($inverseJoinColumn['onDelete']) && strtolower($inverseJoinColumn['onDelete']) == 'cascade') { if (isset($inverseJoinColumn['onDelete']) && strtolower($inverseJoinColumn['onDelete']) == 'cascade') {
$this->inverseIsOnDeleteCascade = true; $this->isOnDeleteCascade = true;
} }
$this->relationToTargetKeyColumns[$inverseJoinColumn['name']] = $inverseJoinColumn['referencedColumnName']; $this->relationToTargetKeyColumns[$inverseJoinColumn['name']] = $inverseJoinColumn['referencedColumnName'];
@ -191,11 +184,8 @@ class ManyToManyMapping extends AssociationMapping
$serialized[] = 'joinTableColumns'; $serialized[] = 'joinTableColumns';
$serialized[] = 'relationToSourceKeyColumns'; $serialized[] = 'relationToSourceKeyColumns';
$serialized[] = 'relationToTargetKeyColumns'; $serialized[] = 'relationToTargetKeyColumns';
if ($this->owningIsOnDeleteCascade) { if ($this->isOnDeleteCascade) {
$serialized[] = 'owningIsOnDeleteCascade'; $serialized[] = 'isOnDeleteCascade';
}
if ($this->inverseIsOnDeleteCascade) {
$serialized[] = 'inverseIsOnDeleteCascade';
} }
if ($this->orderBy) { if ($this->orderBy) {
$serialized[] = 'orderBy'; $serialized[] = 'orderBy';

View file

@ -345,21 +345,16 @@ class BasicEntityPersister
foreach ($this->_class->associationMappings AS $mapping) { foreach ($this->_class->associationMappings AS $mapping) {
/* @var $mapping \Doctrine\ORM\Mapping\AssociationMapping */ /* @var $mapping \Doctrine\ORM\Mapping\AssociationMapping */
if ($mapping->isManyToMany()) { if ($mapping->isManyToMany()) {
if($mapping->isOwningSide && (!$mapping->owningIsOnDeleteCascade || !$this->_platform->supportsForeignKeyConstraints())) { if (!$mapping->isOwningSide) {
$this->_conn->delete(
$mapping->joinTable['name'],
array_combine(array_keys($mapping->relationToSourceKeyColumns), $identifier)
);
} else if (!$mapping->isOwningSide) {
$relatedClass = $this->_em->getClassMetadata($mapping->targetEntityName); $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()) { if(!$mapping->isOnDeleteCascade) {
$this->_conn->delete( $this->_conn->delete($mapping->joinTable['name'], array_combine($keys, $identifier));
$relatedMapping->joinTable['name'],
array_combine(array_keys($relatedMapping->relationToTargetKeyColumns), $identifier)
);
}
} }
} }
} }

View file

@ -107,8 +107,7 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase
'joinColumns' => array(array('name' => 'CmsUser_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE')), 'joinColumns' => array(array('name' => 'CmsUser_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE')),
'inverseJoinColumns' => array(array('name' => 'CmsGroup_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE')) 'inverseJoinColumns' => array(array('name' => 'CmsGroup_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE'))
), $assoc->joinTable); ), $assoc->joinTable);
$this->assertTrue($assoc->owningIsOnDeleteCascade); $this->assertTrue($assoc->isOnDeleteCascade);
$this->assertTrue($assoc->inverseIsOnDeleteCascade);
} }
public function testSerializeManyToManyJoinTableCascade() public function testSerializeManyToManyJoinTableCascade()
@ -124,8 +123,7 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase
$assoc = $cm->associationMappings['groups']; $assoc = $cm->associationMappings['groups'];
$assoc = unserialize(serialize($assoc)); $assoc = unserialize(serialize($assoc));
$this->assertTrue($assoc->owningIsOnDeleteCascade); $this->assertTrue($assoc->isOnDeleteCascade);
$this->assertTrue($assoc->inverseIsOnDeleteCascade);
} }
/** /**