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

code refactoring on ManyToManyPersister

This commit is contained in:
Fabio B. Silva 2012-07-27 21:23:40 -03:00
parent 07492bda9d
commit 2b1aaebe18

View file

@ -40,9 +40,10 @@ class ManyToManyPersister extends AbstractCollectionPersister
*/ */
protected function getDeleteRowSQL(PersistentCollection $coll) protected function getDeleteRowSQL(PersistentCollection $coll)
{ {
$columns = array(); $columns = array();
$mapping = $coll->getMapping(); $mapping = $coll->getMapping();
$class = $this->em->getClassMetadata(get_class($coll->getOwner())); $class = $this->em->getClassMetadata(get_class($coll->getOwner()));
$tableName = $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform);
foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) { foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) {
$columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); $columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
@ -52,7 +53,7 @@ class ManyToManyPersister extends AbstractCollectionPersister
$columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); $columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
} }
return 'DELETE FROM ' . $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform) return 'DELETE FROM ' . $tableName
. ' WHERE ' . implode(' = ? AND ', $columns) . ' = ?'; . ' WHERE ' . implode(' = ? AND ', $columns) . ' = ?';
} }
@ -328,8 +329,9 @@ class ManyToManyPersister extends AbstractCollectionPersister
*/ */
private function getJoinTableRestrictions(PersistentCollection $coll, $element, $addFilters) private function getJoinTableRestrictions(PersistentCollection $coll, $element, $addFilters)
{ {
$uow = $this->em->getUnitOfWork(); $uow = $this->em->getUnitOfWork();
$mapping = $filterMapping = $coll->getMapping(); $filterMapping = $coll->getMapping();
$mapping = $filterMapping;
if ( ! $mapping['isOwningSide']) { if ( ! $mapping['isOwningSide']) {
$sourceClass = $this->em->getClassMetadata($mapping['targetEntity']); $sourceClass = $this->em->getClassMetadata($mapping['targetEntity']);
@ -392,32 +394,38 @@ class ManyToManyPersister extends AbstractCollectionPersister
public function getFilterSql($mapping) public function getFilterSql($mapping)
{ {
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']); $targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
$rootClass = $this->em->getClassMetadata($targetClass->rootEntityName);
$filterSql = $this->generateFilterConditionSQL($rootClass, 'te');
if ($mapping['isOwningSide']) { if ('' === $filterSql) {
$joinColumns = $mapping['relationToTargetKeyColumns']; return array('', '');
} else {
$mapping = $targetClass->associationMappings[$mapping['mappedBy']];
$joinColumns = $mapping['relationToSourceKeyColumns'];
} }
$targetClass = $this->em->getClassMetadata($targetClass->rootEntityName); $conditions = array();
$association = $mapping;
if ( ! $mapping['isOwningSide']) {
$class = $this->em->getClassMetadata($mapping['targetEntity']);
$association = $class->associationMappings[$mapping['mappedBy']];
}
// A join is needed if there is filtering on the target entity // A join is needed if there is filtering on the target entity
$joinTargetEntitySQL = ''; $tableName = $this->quoteStrategy->getTableName($rootClass, $this->platform);
if ($filterSql = $this->generateFilterConditionSQL($targetClass, 'te')) { $joinSql = ' JOIN ' . $tableName . ' te' . ' ON';
$joinTargetEntitySQL = ' JOIN ' $joinColumns = $mapping['isOwningSide']
. $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' te' ? $association['joinTable']['inverseJoinColumns']
. ' ON'; : $association['joinTable']['joinColumns'];
$joinTargetEntitySQLClauses = array(); foreach ($joinColumns as $joinColumn) {
foreach ($joinColumns as $joinTableColumn => $targetTableColumn) { $joinColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform);
$joinTargetEntitySQLClauses[] = ' t.' . $joinTableColumn . ' = ' . 'te.' . $targetTableColumn; $refColumnName = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform);
}
$joinTargetEntitySQL .= implode(' AND ', $joinTargetEntitySQLClauses); $conditions[] = ' t.' . $joinColumnName . ' = ' . 'te.' . $refColumnName;
} }
return array($joinTargetEntitySQL, $filterSql); $joinSql .= implode(' AND ', $conditions);
return array($joinSql, $filterSql);
} }
/** /**