1
0
mirror of synced 2025-01-19 06:51:40 +03:00

Moved delete() and update() to proper locations.

This commit is contained in:
Guilherme Blanco 2015-01-12 22:04:04 +00:00
parent b9c0868f08
commit dd883f2136
6 changed files with 364 additions and 513 deletions

View File

@ -226,22 +226,6 @@ abstract class AbstractCollectionPersister implements CachedCollectionPersister
return $this->persister->count($collection); return $this->persister->count($collection);
} }
/**
* {@inheritdoc}
*/
public function deleteRows(PersistentCollection $collection)
{
$this->persister->deleteRows($collection);
}
/**
* {@inheritdoc}
*/
public function insertRows(PersistentCollection $collection)
{
$this->persister->insertRows($collection);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -79,73 +79,7 @@ abstract class AbstractCollectionPersister implements CollectionPersister
*/ */
public function delete(PersistentCollection $coll) public function delete(PersistentCollection $coll)
{ {
$mapping = $coll->getMapping(); throw new \BadMethodCallException("Deleting elements is not supported by this CollectionPersister.");
if ( ! $mapping['isOwningSide']) {
return; // ignore inverse side
}
$this->conn->executeUpdate($this->getDeleteSQL($coll), $this->getDeleteSQLParameters($coll));
}
/**
* Gets the SQL statement for deleting the given collection.
*
* @param \Doctrine\ORM\PersistentCollection $coll
*
* @return string
*/
abstract protected function getDeleteSQL(PersistentCollection $coll);
/**
* Gets the SQL parameters for the corresponding SQL statement to delete
* the given collection.
*
* @param \Doctrine\ORM\PersistentCollection $coll
*
* @return array
*/
abstract protected function getDeleteSQLParameters(PersistentCollection $coll);
/**
* {@inheritdoc}
*/
public function update(PersistentCollection $coll)
{
$mapping = $coll->getMapping();
if ( ! $mapping['isOwningSide']) {
return; // ignore inverse side
}
$this->deleteRows($coll);
$this->insertRows($coll);
}
/**
* {@inheritdoc}
*/
public function deleteRows(PersistentCollection $coll)
{
$diff = $coll->getDeleteDiff();
$sql = $this->getDeleteRowSQL($coll);
foreach ($diff as $element) {
$this->conn->executeUpdate($sql, $this->getDeleteRowSQLParameters($coll, $element));
}
}
/**
* {@inheritdoc}
*/
public function insertRows(PersistentCollection $coll)
{
$diff = $coll->getInsertDiff();
$sql = $this->getInsertRowSQL($coll);
foreach ($diff as $element) {
$this->conn->executeUpdate($sql, $this->getInsertRowSQLParameters($coll, $element));
}
} }
/** /**
@ -211,53 +145,4 @@ abstract class AbstractCollectionPersister implements CollectionPersister
{ {
throw new \BadMethodCallException("Filtering a collection by Criteria is not supported by this CollectionPersister."); throw new \BadMethodCallException("Filtering a collection by Criteria is not supported by this CollectionPersister.");
} }
/**
* Gets the SQL statement used for deleting a row from the collection.
*
* @param \Doctrine\ORM\PersistentCollection $coll
*
* @return string
*/
abstract protected function getDeleteRowSQL(PersistentCollection $coll);
/**
* Gets the SQL parameters for the corresponding SQL statement to delete the given
* element from the given collection.
*
* @param \Doctrine\ORM\PersistentCollection $coll
* @param mixed $element
*
* @return array
*/
abstract protected function getDeleteRowSQLParameters(PersistentCollection $coll, $element);
/**
* Gets the SQL statement used for updating a row in the collection.
*
* @param \Doctrine\ORM\PersistentCollection $coll
*
* @return string
*/
abstract protected function getUpdateRowSQL(PersistentCollection $coll);
/**
* Gets the SQL statement used for inserting a row in the collection.
*
* @param \Doctrine\ORM\PersistentCollection $coll
*
* @return string
*/
abstract protected function getInsertRowSQL(PersistentCollection $coll);
/**
* Gets the SQL parameters for the corresponding SQL statement to insert the given
* element of the given collection into the database.
*
* @param \Doctrine\ORM\PersistentCollection $coll
* @param mixed $element
*
* @return array
*/
abstract protected function getInsertRowSQLParameters(PersistentCollection $coll, $element);
} }

View File

@ -50,24 +50,6 @@ interface CollectionPersister
*/ */
public function update(PersistentCollection $collection); public function update(PersistentCollection $collection);
/**
* Deletes rows.
*
* @param \Doctrine\ORM\PersistentCollection $collection
*
* @return void
*/
public function deleteRows(PersistentCollection $collection);
/**
* Inserts rows.
*
* @param \Doctrine\ORM\PersistentCollection $collection
*
* @return void
*/
public function insertRows(PersistentCollection $collection);
/** /**
* Counts the size of this persistent collection. * Counts the size of this persistent collection.
* *

View File

@ -37,180 +37,42 @@ class ManyToManyPersister extends AbstractCollectionPersister
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @override
*/ */
protected function getDeleteRowSQL(PersistentCollection $coll) public function delete(PersistentCollection $coll)
{ {
$columns = array(); $mapping = $coll->getMapping();
$mapping = $coll->getMapping();
$class = $this->em->getClassMetadata(get_class($coll->getOwner()));
$tableName = $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform);
foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) { if ( ! $mapping['isOwningSide']) {
$columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); return; // ignore inverse side
} }
foreach ($mapping['joinTable']['inverseJoinColumns'] as $joinColumn) { $this->conn->executeUpdate($this->getDeleteSQL($coll), $this->getDeleteSQLParameters($coll));
$columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
}
return 'DELETE FROM ' . $tableName
. ' WHERE ' . implode(' = ? AND ', $columns) . ' = ?';
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @override
*
* @internal Order of the parameters must be the same as the order of the columns in getDeleteRowSql.
*/ */
protected function getDeleteRowSQLParameters(PersistentCollection $coll, $element) public function update(PersistentCollection $coll)
{ {
return $this->collectJoinTableColumnParameters($coll, $element); $mapping = $coll->getMapping();
}
/** if ( ! $mapping['isOwningSide']) {
* {@inheritdoc} return; // ignore inverse side
*
* @throws \BadMethodCallException Not used for OneToManyPersister
*/
protected function getUpdateRowSQL(PersistentCollection $coll)
{
throw new \BadMethodCallException("Insert Row SQL is not used for ManyToManyPersister");
}
/**
* {@inheritdoc}
*
* @override
*
* @internal Order of the parameters must be the same as the order of the columns in getInsertRowSql.
*/
protected function getInsertRowSQL(PersistentCollection $coll)
{
$columns = array();
$mapping = $coll->getMapping();
$class = $this->em->getClassMetadata(get_class($coll->getOwner()));
$joinTable = $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform);
foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) {
$columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
} }
foreach ($mapping['joinTable']['inverseJoinColumns'] as $joinColumn) { $diff = $coll->getDeleteDiff();
$columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); $sql = $this->getDeleteRowSQL($coll);
foreach ($diff as $element) {
$this->conn->executeUpdate($sql, $this->getDeleteRowSQLParameters($coll, $element));
} }
return 'INSERT INTO ' . $joinTable . ' (' . implode(', ', $columns) . ')' $diff = $coll->getInsertDiff();
. ' VALUES (' . implode(', ', array_fill(0, count($columns), '?')) . ')'; $sql = $this->getInsertRowSQL($coll);
}
/** foreach ($diff as $element) {
* {@inheritdoc} $this->conn->executeUpdate($sql, $this->getInsertRowSQLParameters($coll, $element));
*
* @override
*
* @internal Order of the parameters must be the same as the order of the columns in getInsertRowSql.
*/
protected function getInsertRowSQLParameters(PersistentCollection $coll, $element)
{
return $this->collectJoinTableColumnParameters($coll, $element);
}
/**
* Collects the parameters for inserting/deleting on the join table in the order
* of the join table columns as specified in ManyToManyMapping#joinTableColumns.
*
* @param \Doctrine\ORM\PersistentCollection $coll
* @param object $element
*
* @return array
*/
private function collectJoinTableColumnParameters(PersistentCollection $coll, $element)
{
$params = array();
$mapping = $coll->getMapping();
$isComposite = count($mapping['joinTableColumns']) > 2;
$identifier1 = $this->uow->getEntityIdentifier($coll->getOwner());
$identifier2 = $this->uow->getEntityIdentifier($element);
if ($isComposite) {
$class1 = $this->em->getClassMetadata(get_class($coll->getOwner()));
$class2 = $coll->getTypeClass();
} }
foreach ($mapping['joinTableColumns'] as $joinTableColumn) {
$isRelationToSource = isset($mapping['relationToSourceKeyColumns'][$joinTableColumn]);
if ( ! $isComposite) {
$params[] = $isRelationToSource ? array_pop($identifier1) : array_pop($identifier2);
continue;
}
if ($isRelationToSource) {
$params[] = $identifier1[$class1->getFieldForColumn($mapping['relationToSourceKeyColumns'][$joinTableColumn])];
continue;
}
$params[] = $identifier2[$class2->getFieldForColumn($mapping['relationToTargetKeyColumns'][$joinTableColumn])];
}
return $params;
}
/**
* {@inheritdoc}
*
* @override
*/
protected function getDeleteSQL(PersistentCollection $coll)
{
$columns = array();
$mapping = $coll->getMapping();
$class = $this->em->getClassMetadata(get_class($coll->getOwner()));
$joinTable = $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform);
foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) {
$columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
}
return 'DELETE FROM ' . $joinTable
. ' WHERE ' . implode(' = ? AND ', $columns) . ' = ?';
}
/**
* {@inheritdoc}
*
* @override
*
* @internal Order of the parameters must be the same as the order of the columns in getDeleteSql.
*/
protected function getDeleteSQLParameters(PersistentCollection $coll)
{
$mapping = $coll->getMapping();
$identifier = $this->uow->getEntityIdentifier($coll->getOwner());
// Optimization for single column identifier
if (count($mapping['relationToSourceKeyColumns']) === 1) {
return array(reset($identifier));
}
// Composite identifier
$sourceClass = $this->em->getClassMetadata($mapping['sourceEntity']);
$params = array();
foreach ($mapping['relationToSourceKeyColumns'] as $columnName => $refColumnName) {
$params[] = isset($sourceClass->fieldNames[$refColumnName])
? $identifier[$sourceClass->fieldNames[$refColumnName]]
: $identifier[$sourceClass->getFieldForColumn($columnName)];
}
return $params;
} }
/** /**
@ -328,135 +190,46 @@ class ManyToManyPersister extends AbstractCollectionPersister
} }
/** /**
* @param \Doctrine\ORM\PersistentCollection $coll * {@inheritDoc}
* @param string $key
* @param boolean $addFilters Whether the filter SQL should be included or not.
*
* @return array
*/ */
private function getJoinTableRestrictionsWithKey(PersistentCollection $coll, $key, $addFilters) public function loadCriteria(PersistentCollection $coll, Criteria $criteria)
{ {
$filterMapping = $coll->getMapping(); $mapping = $coll->getMapping();
$mapping = $filterMapping; $owner = $coll->getOwner();
$indexBy = $mapping['indexBy']; $ownerMetadata = $this->em->getClassMetadata(get_class($owner));
$id = $this->uow->getEntityIdentifier($coll->getOwner()); $whereClauses = $params = array();
$targetEntity = $this->em->getClassMetadata($mapping['targetEntity']); foreach ($mapping['relationToSourceKeyColumns'] as $key => $value) {
$whereClauses[] = sprintf('t.%s = ?', $key);
if (! $mapping['isOwningSide']) { $params[] = $ownerMetadata->getFieldValue($owner, $value);
$associationSourceClass = $this->em->getClassMetadata($mapping['targetEntity']);
$mapping = $associationSourceClass->associationMappings[$mapping['mappedBy']];
$joinColumns = $mapping['joinTable']['joinColumns'];
$relationMode = 'relationToTargetKeyColumns';
} else {
$joinColumns = $mapping['joinTable']['inverseJoinColumns'];
$associationSourceClass = $this->em->getClassMetadata($mapping['sourceEntity']);
$relationMode = 'relationToSourceKeyColumns';
} }
$quotedJoinTable = $this->quoteStrategy->getJoinTableName($mapping, $associationSourceClass, $this->platform). ' t'; $parameters = $this->expandCriteriaParameters($criteria);
$whereClauses = array();
$params = array();
$joinNeeded = !in_array($indexBy, $targetEntity->identifier);
if ($joinNeeded) { // extra join needed if indexBy is not a @id
$joinConditions = array();
foreach ($joinColumns as $joinTableColumn) {
$joinConditions[] = 't.' . $joinTableColumn['name'] . ' = tr.' . $joinTableColumn['referencedColumnName'];
}
$tableName = $this->quoteStrategy->getTableName($targetEntity, $this->platform);
$quotedJoinTable .= ' JOIN ' . $tableName . ' tr ON ' . implode(' AND ', $joinConditions);
$whereClauses[] = 'tr.' . $targetEntity->getColumnName($indexBy) . ' = ?';
$params[] = $key;
foreach ($parameters as $parameter) {
list($name, $value) = $parameter;
$whereClauses[] = sprintf('te.%s = ?', $name);
$params[] = $value;
} }
foreach ($mapping['joinTableColumns'] as $joinTableColumn) { $mapping = $coll->getMapping();
if (isset($mapping[$relationMode][$joinTableColumn])) { $targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
$whereClauses[] = 't.' . $joinTableColumn . ' = ?'; $tableName = $this->quoteStrategy->getTableName($targetClass, $this->platform);
$params[] = $targetEntity->containsForeignIdentifier $joinTable = $this->quoteStrategy->getJoinTableName($mapping, $ownerMetadata, $this->platform);
? $id[$targetEntity->getFieldForColumn($mapping[$relationMode][$joinTableColumn])] $onConditions = $this->getOnConditionSQL($mapping);
: $id[$targetEntity->fieldNames[$mapping[$relationMode][$joinTableColumn]]];
} elseif (!$joinNeeded) {
$whereClauses[] = 't.' . $joinTableColumn . ' = ?';
$params[] = $key;
}
}
if ($addFilters) { $rsm = new Query\ResultSetMappingBuilder($this->em);
list($joinTargetEntitySQL, $filterSql) = $this->getFilterSql($filterMapping); $rsm->addRootEntityFromClassMetadata($mapping['targetEntity'], 'te');
if ($filterSql) { $sql = 'SELECT ' . $rsm->generateSelectClause() . ' FROM ' . $tableName . ' te'
$quotedJoinTable .= ' ' . $joinTargetEntitySQL; . ' JOIN ' . $joinTable . ' t ON'
$whereClauses[] = $filterSql; . implode(' AND ', $onConditions)
} . ' WHERE ' . implode(' AND ', $whereClauses);
}
return array($quotedJoinTable, $whereClauses, $params); $stmt = $this->conn->executeQuery($sql, $params);
} $hydrator = $this->em->newHydrator(Query::HYDRATE_OBJECT);
/** return $hydrator->hydrateAll($stmt, $rsm);
* @param \Doctrine\ORM\PersistentCollection $coll
* @param object $element
* @param boolean $addFilters Whether the filter SQL should be included or not.
*
* @return array
*/
private function getJoinTableRestrictions(PersistentCollection $coll, $element, $addFilters)
{
$filterMapping = $coll->getMapping();
$mapping = $filterMapping;
if ( ! $mapping['isOwningSide']) {
$sourceClass = $this->em->getClassMetadata($mapping['targetEntity']);
$targetClass = $this->em->getClassMetadata($mapping['sourceEntity']);
$sourceId = $this->uow->getEntityIdentifier($element);
$targetId = $this->uow->getEntityIdentifier($coll->getOwner());
$mapping = $sourceClass->associationMappings[$mapping['mappedBy']];
} else {
$sourceClass = $this->em->getClassMetadata($mapping['sourceEntity']);
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
$sourceId = $this->uow->getEntityIdentifier($coll->getOwner());
$targetId = $this->uow->getEntityIdentifier($element);
}
$quotedJoinTable = $this->quoteStrategy->getJoinTableName($mapping, $sourceClass, $this->platform);
$whereClauses = array();
$params = array();
foreach ($mapping['joinTableColumns'] as $joinTableColumn) {
$whereClauses[] = ($addFilters ? 't.' : '') . $joinTableColumn . ' = ?';
if (isset($mapping['relationToTargetKeyColumns'][$joinTableColumn])) {
$params[] = ($targetClass->containsForeignIdentifier)
? $targetId[$targetClass->getFieldForColumn($mapping['relationToTargetKeyColumns'][$joinTableColumn])]
: $targetId[$targetClass->fieldNames[$mapping['relationToTargetKeyColumns'][$joinTableColumn]]];
continue;
}
// relationToSourceKeyColumns
$params[] = ($sourceClass->containsForeignIdentifier)
? $sourceId[$sourceClass->getFieldForColumn($mapping['relationToSourceKeyColumns'][$joinTableColumn])]
: $sourceId[$sourceClass->fieldNames[$mapping['relationToSourceKeyColumns'][$joinTableColumn]]];
}
if ($addFilters) {
$quotedJoinTable .= ' t';
list($joinTargetEntitySQL, $filterSql) = $this->getFilterSql($filterMapping);
if ($filterSql) {
$quotedJoinTable .= ' ' . $joinTargetEntitySQL;
$whereClauses[] = $filterSql;
}
}
return array($quotedJoinTable, $whereClauses, $params);
} }
/** /**
@ -549,46 +322,313 @@ class ManyToManyPersister extends AbstractCollectionPersister
} }
/** /**
* {@inheritDoc} * {@inheritdoc}
*
* @override
*/ */
public function loadCriteria(PersistentCollection $coll, Criteria $criteria) protected function getDeleteSQL(PersistentCollection $coll)
{ {
$mapping = $coll->getMapping(); $columns = array();
$owner = $coll->getOwner(); $mapping = $coll->getMapping();
$ownerMetadata = $this->em->getClassMetadata(get_class($owner)); $class = $this->em->getClassMetadata(get_class($coll->getOwner()));
$whereClauses = $params = array(); $joinTable = $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform);
foreach ($mapping['relationToSourceKeyColumns'] as $key => $value) { foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) {
$whereClauses[] = sprintf('t.%s = ?', $key); $columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
$params[] = $ownerMetadata->getFieldValue($owner, $value);
} }
$parameters = $this->expandCriteriaParameters($criteria); return 'DELETE FROM ' . $joinTable
. ' WHERE ' . implode(' = ? AND ', $columns) . ' = ?';
}
foreach ($parameters as $parameter) { /**
list($name, $value) = $parameter; * {@inheritdoc}
$whereClauses[] = sprintf('te.%s = ?', $name); *
$params[] = $value; * @override
*
* @internal Order of the parameters must be the same as the order of the columns in getDeleteSql.
*/
protected function getDeleteSQLParameters(PersistentCollection $coll)
{
$mapping = $coll->getMapping();
$identifier = $this->uow->getEntityIdentifier($coll->getOwner());
// Optimization for single column identifier
if (count($mapping['relationToSourceKeyColumns']) === 1) {
return array(reset($identifier));
} }
$mapping = $coll->getMapping(); // Composite identifier
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']); $sourceClass = $this->em->getClassMetadata($mapping['sourceEntity']);
$tableName = $this->quoteStrategy->getTableName($targetClass, $this->platform); $params = array();
$joinTable = $this->quoteStrategy->getJoinTableName($mapping, $ownerMetadata, $this->platform);
$onConditions = $this->getOnConditionSQL($mapping);
$rsm = new Query\ResultSetMappingBuilder($this->em); foreach ($mapping['relationToSourceKeyColumns'] as $columnName => $refColumnName) {
$rsm->addRootEntityFromClassMetadata($mapping['targetEntity'], 'te'); $params[] = isset($sourceClass->fieldNames[$refColumnName])
? $identifier[$sourceClass->fieldNames[$refColumnName]]
: $identifier[$sourceClass->getFieldForColumn($columnName)];
}
$sql = 'SELECT ' . $rsm->generateSelectClause() . ' FROM ' . $tableName . ' te' return $params;
. ' JOIN ' . $joinTable . ' t ON' }
. implode(' AND ', $onConditions)
. ' WHERE ' . implode(' AND ', $whereClauses);
$stmt = $this->conn->executeQuery($sql, $params); /**
$hydrator = $this->em->newHydrator(Query::HYDRATE_OBJECT); * Gets the SQL statement used for deleting a row from the collection.
*
* @param \Doctrine\ORM\PersistentCollection $coll
*
* @return string
*/
protected function getDeleteRowSQL(PersistentCollection $coll)
{
$columns = array();
$mapping = $coll->getMapping();
$class = $this->em->getClassMetadata(get_class($coll->getOwner()));
$tableName = $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform);
return $hydrator->hydrateAll($stmt, $rsm); foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) {
$columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
}
foreach ($mapping['joinTable']['inverseJoinColumns'] as $joinColumn) {
$columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
}
return 'DELETE FROM ' . $tableName
. ' WHERE ' . implode(' = ? AND ', $columns) . ' = ?';
}
/**
* Gets the SQL parameters for the corresponding SQL statement to delete the given
* element from the given collection.
*
* @internal Order of the parameters must be the same as the order of the columns in getDeleteRowSql.
*
* @param \Doctrine\ORM\PersistentCollection $coll
* @param mixed $element
*
* @return array
*/
protected function getDeleteRowSQLParameters(PersistentCollection $coll, $element)
{
return $this->collectJoinTableColumnParameters($coll, $element);
}
/**
* Gets the SQL statement used for inserting a row in the collection.
*
* @param \Doctrine\ORM\PersistentCollection $coll
*
* @return string
*/
protected function getInsertRowSQL(PersistentCollection $coll)
{
$columns = array();
$mapping = $coll->getMapping();
$class = $this->em->getClassMetadata(get_class($coll->getOwner()));
$joinTable = $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform);
foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) {
$columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
}
foreach ($mapping['joinTable']['inverseJoinColumns'] as $joinColumn) {
$columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
}
return 'INSERT INTO ' . $joinTable . ' (' . implode(', ', $columns) . ')'
. ' VALUES (' . implode(', ', array_fill(0, count($columns), '?')) . ')';
}
/**
* Gets the SQL parameters for the corresponding SQL statement to insert the given
* element of the given collection into the database.
*
* @internal Order of the parameters must be the same as the order of the columns in getInsertRowSql.
*
* @param \Doctrine\ORM\PersistentCollection $coll
* @param mixed $element
*
* @return array
*/
protected function getInsertRowSQLParameters(PersistentCollection $coll, $element)
{
return $this->collectJoinTableColumnParameters($coll, $element);
}
/**
* Collects the parameters for inserting/deleting on the join table in the order
* of the join table columns as specified in ManyToManyMapping#joinTableColumns.
*
* @param \Doctrine\ORM\PersistentCollection $coll
* @param object $element
*
* @return array
*/
private function collectJoinTableColumnParameters(PersistentCollection $coll, $element)
{
$params = array();
$mapping = $coll->getMapping();
$isComposite = count($mapping['joinTableColumns']) > 2;
$identifier1 = $this->uow->getEntityIdentifier($coll->getOwner());
$identifier2 = $this->uow->getEntityIdentifier($element);
if ($isComposite) {
$class1 = $this->em->getClassMetadata(get_class($coll->getOwner()));
$class2 = $coll->getTypeClass();
}
foreach ($mapping['joinTableColumns'] as $joinTableColumn) {
$isRelationToSource = isset($mapping['relationToSourceKeyColumns'][$joinTableColumn]);
if ( ! $isComposite) {
$params[] = $isRelationToSource ? array_pop($identifier1) : array_pop($identifier2);
continue;
}
if ($isRelationToSource) {
$params[] = $identifier1[$class1->getFieldForColumn($mapping['relationToSourceKeyColumns'][$joinTableColumn])];
continue;
}
$params[] = $identifier2[$class2->getFieldForColumn($mapping['relationToTargetKeyColumns'][$joinTableColumn])];
}
return $params;
}
/**
* @param \Doctrine\ORM\PersistentCollection $coll
* @param string $key
* @param boolean $addFilters Whether the filter SQL should be included or not.
*
* @return array
*/
private function getJoinTableRestrictionsWithKey(PersistentCollection $coll, $key, $addFilters)
{
$filterMapping = $coll->getMapping();
$mapping = $filterMapping;
$indexBy = $mapping['indexBy'];
$id = $this->uow->getEntityIdentifier($coll->getOwner());
$targetEntity = $this->em->getClassMetadata($mapping['targetEntity']);
if (! $mapping['isOwningSide']) {
$associationSourceClass = $this->em->getClassMetadata($mapping['targetEntity']);
$mapping = $associationSourceClass->associationMappings[$mapping['mappedBy']];
$joinColumns = $mapping['joinTable']['joinColumns'];
$relationMode = 'relationToTargetKeyColumns';
} else {
$joinColumns = $mapping['joinTable']['inverseJoinColumns'];
$associationSourceClass = $this->em->getClassMetadata($mapping['sourceEntity']);
$relationMode = 'relationToSourceKeyColumns';
}
$quotedJoinTable = $this->quoteStrategy->getJoinTableName($mapping, $associationSourceClass, $this->platform). ' t';
$whereClauses = array();
$params = array();
$joinNeeded = !in_array($indexBy, $targetEntity->identifier);
if ($joinNeeded) { // extra join needed if indexBy is not a @id
$joinConditions = array();
foreach ($joinColumns as $joinTableColumn) {
$joinConditions[] = 't.' . $joinTableColumn['name'] . ' = tr.' . $joinTableColumn['referencedColumnName'];
}
$tableName = $this->quoteStrategy->getTableName($targetEntity, $this->platform);
$quotedJoinTable .= ' JOIN ' . $tableName . ' tr ON ' . implode(' AND ', $joinConditions);
$whereClauses[] = 'tr.' . $targetEntity->getColumnName($indexBy) . ' = ?';
$params[] = $key;
}
foreach ($mapping['joinTableColumns'] as $joinTableColumn) {
if (isset($mapping[$relationMode][$joinTableColumn])) {
$whereClauses[] = 't.' . $joinTableColumn . ' = ?';
$params[] = $targetEntity->containsForeignIdentifier
? $id[$targetEntity->getFieldForColumn($mapping[$relationMode][$joinTableColumn])]
: $id[$targetEntity->fieldNames[$mapping[$relationMode][$joinTableColumn]]];
} elseif (!$joinNeeded) {
$whereClauses[] = 't.' . $joinTableColumn . ' = ?';
$params[] = $key;
}
}
if ($addFilters) {
list($joinTargetEntitySQL, $filterSql) = $this->getFilterSql($filterMapping);
if ($filterSql) {
$quotedJoinTable .= ' ' . $joinTargetEntitySQL;
$whereClauses[] = $filterSql;
}
}
return array($quotedJoinTable, $whereClauses, $params);
}
/**
* @param \Doctrine\ORM\PersistentCollection $coll
* @param object $element
* @param boolean $addFilters Whether the filter SQL should be included or not.
*
* @return array
*/
private function getJoinTableRestrictions(PersistentCollection $coll, $element, $addFilters)
{
$filterMapping = $coll->getMapping();
$mapping = $filterMapping;
if ( ! $mapping['isOwningSide']) {
$sourceClass = $this->em->getClassMetadata($mapping['targetEntity']);
$targetClass = $this->em->getClassMetadata($mapping['sourceEntity']);
$sourceId = $this->uow->getEntityIdentifier($element);
$targetId = $this->uow->getEntityIdentifier($coll->getOwner());
$mapping = $sourceClass->associationMappings[$mapping['mappedBy']];
} else {
$sourceClass = $this->em->getClassMetadata($mapping['sourceEntity']);
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
$sourceId = $this->uow->getEntityIdentifier($coll->getOwner());
$targetId = $this->uow->getEntityIdentifier($element);
}
$quotedJoinTable = $this->quoteStrategy->getJoinTableName($mapping, $sourceClass, $this->platform);
$whereClauses = array();
$params = array();
foreach ($mapping['joinTableColumns'] as $joinTableColumn) {
$whereClauses[] = ($addFilters ? 't.' : '') . $joinTableColumn . ' = ?';
if (isset($mapping['relationToTargetKeyColumns'][$joinTableColumn])) {
$params[] = ($targetClass->containsForeignIdentifier)
? $targetId[$targetClass->getFieldForColumn($mapping['relationToTargetKeyColumns'][$joinTableColumn])]
: $targetId[$targetClass->fieldNames[$mapping['relationToTargetKeyColumns'][$joinTableColumn]]];
continue;
}
// relationToSourceKeyColumns
$params[] = ($sourceClass->containsForeignIdentifier)
? $sourceId[$sourceClass->getFieldForColumn($mapping['relationToSourceKeyColumns'][$joinTableColumn])]
: $sourceId[$sourceClass->fieldNames[$mapping['relationToSourceKeyColumns'][$joinTableColumn]]];
}
if ($addFilters) {
$quotedJoinTable .= ' t';
list($joinTargetEntitySQL, $filterSql) = $this->getFilterSql($filterMapping);
if ($filterSql) {
$quotedJoinTable .= ' ' . $joinTargetEntitySQL;
$whereClauses[] = $filterSql;
}
}
return array($quotedJoinTable, $whereClauses, $params);
} }
/** /**

View File

@ -33,6 +33,28 @@ use Doctrine\ORM\UnitOfWork;
*/ */
class OneToManyPersister extends AbstractCollectionPersister class OneToManyPersister extends AbstractCollectionPersister
{ {
/**
* {@inheritdoc}
*/
public function delete(PersistentCollection $coll)
{
// This can never happen. One to many can only be inverse side.
// For owning side one to many, it is required to have a join table,
// then classifying it as a ManyToManyPersister.
return;
}
/**
* {@inheritdoc}
*/
public function update(PersistentCollection $coll)
{
// This can never happen. One to many can only be inverse side.
// For owning side one to many, it is required to have a join table,
// then classifying it as a ManyToManyPersister.
return;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -97,36 +119,6 @@ class OneToManyPersister extends AbstractCollectionPersister
throw new \BadMethodCallException("Insert Row SQL is not used for OneToManyPersister"); throw new \BadMethodCallException("Insert Row SQL is not used for OneToManyPersister");
} }
/**
* {@inheritdoc}
*
* @throws \BadMethodCallException Not used for OneToManyPersister.
*/
protected function getUpdateRowSQL(PersistentCollection $coll)
{
throw new \BadMethodCallException("Update Row SQL is not used for OneToManyPersister");
}
/**
* {@inheritdoc}
*
* @throws \BadMethodCallException Not used for OneToManyPersister.
*/
protected function getDeleteSQL(PersistentCollection $coll)
{
throw new \BadMethodCallException("Delete Row SQL is not used for OneToManyPersister");
}
/**
* {@inheritdoc}
*
* @throws \BadMethodCallException Not used for OneToManyPersister.
*/
protected function getDeleteSQLParameters(PersistentCollection $coll)
{
throw new \BadMethodCallException("Delete Row SQL is not used for OneToManyPersister");
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -49,8 +49,6 @@ abstract class AbstractCollectionPersisterTest extends OrmTestCase
protected $collectionPersisterMockMethods = array( protected $collectionPersisterMockMethods = array(
'delete', 'delete',
'update', 'update',
'deleteRows',
'insertRows',
'count', 'count',
'slice', 'slice',
'contains', 'contains',
@ -154,36 +152,6 @@ abstract class AbstractCollectionPersisterTest extends OrmTestCase
$this->assertNull($persister->update($collection)); $this->assertNull($persister->update($collection));
} }
public function testInvokeDeleteRows()
{
$entity = new State("Foo");
$persister = $this->createPersisterDefault();
$collection = $this->createCollection($entity);
$this->em->getUnitOfWork()->registerManaged($entity, array('id'=>1), array('id'=>1, 'name'=>'Foo'));
$this->collectionPersister->expects($this->once())
->method('deleteRows')
->with($this->equalTo($collection));
$this->assertNull($persister->deleteRows($collection));
}
public function testInvokeInsertRows()
{
$entity = new State("Foo");
$persister = $this->createPersisterDefault();
$collection = $this->createCollection($entity);
$this->em->getUnitOfWork()->registerManaged($entity, array('id'=>1), array('id'=>1, 'name'=>'Foo'));
$this->collectionPersister->expects($this->once())
->method('insertRows')
->with($this->equalTo($collection));
$this->assertNull($persister->insertRows($collection));
}
public function testInvokeCount() public function testInvokeCount()
{ {
$entity = new State("Foo"); $entity = new State("Foo");