1
0
Fork 0
mirror of synced 2025-04-01 12:26:11 +03:00

Merge branch 'optimize-persisters'

Close #1246
This commit is contained in:
Marco Pivetta 2015-01-13 15:05:45 +01:00
commit 9c3cb57931
8 changed files with 420 additions and 683 deletions

View file

@ -226,22 +226,6 @@ abstract class AbstractCollectionPersister implements CachedCollectionPersister
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}
*/

View file

@ -19,9 +19,7 @@
namespace Doctrine\ORM\Persisters;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\PersistentCollection;
/**
* Base class for all collection persisters.
@ -73,191 +71,4 @@ abstract class AbstractCollectionPersister implements CollectionPersister
$this->platform = $this->conn->getDatabasePlatform();
$this->quoteStrategy = $em->getConfiguration()->getQuoteStrategy();
}
/**
* {@inheritdoc}
*/
public function delete(PersistentCollection $coll)
{
$mapping = $coll->getMapping();
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));
}
}
/**
* {@inheritdoc}
*/
public function count(PersistentCollection $coll)
{
throw new \BadMethodCallException("Counting the size of this persistent collection is not supported by this CollectionPersister.");
}
/**
* {@inheritdoc}
*/
public function slice(PersistentCollection $coll, $offset, $length = null)
{
throw new \BadMethodCallException("Slicing elements is not supported by this CollectionPersister.");
}
/**
* {@inheritdoc}
*/
public function contains(PersistentCollection $coll, $element)
{
throw new \BadMethodCallException("Checking for existence of an element is not supported by this CollectionPersister.");
}
/**
* {@inheritdoc}
*/
public function containsKey(PersistentCollection $coll, $key)
{
throw new \BadMethodCallException("Checking for existence of a key is not supported by this CollectionPersister.");
}
/**
* {@inheritdoc}
*/
public function removeElement(PersistentCollection $coll, $element)
{
throw new \BadMethodCallException("Removing an element is not supported by this CollectionPersister.");
}
/**
* {@inheritdoc}
*/
public function removeKey(PersistentCollection $coll, $key)
{
throw new \BadMethodCallException("Removing a key is not supported by this CollectionPersister.");
}
/**
* {@inheritdoc}
*/
public function get(PersistentCollection $coll, $index)
{
throw new \BadMethodCallException("Selecting a collection by index is not supported by this CollectionPersister.");
}
/**
* {@inheritdoc}
*/
public function loadCriteria(PersistentCollection $coll, Criteria $criteria)
{
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

@ -655,6 +655,8 @@ class BasicEntityPersister implements EntityPersister
}
}
$newValId = null;
if ($newVal !== null) {
$newValId = $uow->getEntityIdentifier($newVal);
}
@ -667,24 +669,11 @@ class BasicEntityPersister implements EntityPersister
$targetColumn = $joinColumn['referencedColumnName'];
$quotedColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform);
$this->quotedColumns[$sourceColumn] = $quotedColumn;
$this->columnTypes[$sourceColumn] = $targetClass->getTypeOfColumn($targetColumn);
switch (true) {
case $newVal === null:
$value = null;
break;
case $targetClass->containsForeignIdentifier:
$value = $newValId[$targetClass->getFieldForColumn($targetColumn)];
break;
default:
$value = $newValId[$targetClass->fieldNames[$targetColumn]];
break;
}
$result[$owningTable][$sourceColumn] = $value;
$this->quotedColumns[$sourceColumn] = $quotedColumn;
$this->columnTypes[$sourceColumn] = $targetClass->getTypeOfColumn($targetColumn);
$result[$owningTable][$sourceColumn] = $newValId
? $newValId[$targetClass->getFieldForColumn($targetColumn)]
: null;
}
}

View file

@ -50,24 +50,6 @@ interface CollectionPersister
*/
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.
*
@ -118,16 +100,6 @@ interface CollectionPersister
*/
public function removeElement(PersistentCollection $collection, $element);
/**
* Removes an element by key.
*
* @param \Doctrine\ORM\PersistentCollection $collection
* @param mixed $key
*
* @return void
*/
public function removeKey(PersistentCollection $collection, $key);
/**
* Gets an element by key.
*

View file

@ -37,180 +37,47 @@ class ManyToManyPersister extends AbstractCollectionPersister
{
/**
* {@inheritdoc}
*
* @override
*/
protected function getDeleteRowSQL(PersistentCollection $coll)
public function delete(PersistentCollection $coll)
{
$columns = array();
$mapping = $coll->getMapping();
$class = $this->em->getClassMetadata(get_class($coll->getOwner()));
$tableName = $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform);
$mapping = $coll->getMapping();
foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) {
$columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
if ( ! $mapping['isOwningSide']) {
return; // ignore inverse side
}
foreach ($mapping['joinTable']['inverseJoinColumns'] as $joinColumn) {
$columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
}
return 'DELETE FROM ' . $tableName
. ' WHERE ' . implode(' = ? AND ', $columns) . ' = ?';
$this->conn->executeUpdate($this->getDeleteSQL($coll), $this->getDeleteSQLParameters($coll));
}
/**
* {@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']) {
return; // ignore inverse side
}
$insertSql = $this->getInsertRowSQL($coll);
$deleteSql = $this->getDeleteRowSQL($coll);
foreach ($coll->getDeleteDiff() as $element) {
$this->conn->executeUpdate($deleteSql, $this->getDeleteRowSQLParameters($coll, $element));
}
foreach ($coll->getInsertDiff() as $element) {
$this->conn->executeUpdate($insertSql, $this->getInsertRowSQLParameters($coll, $element));
}
}
/**
* {@inheritdoc}
*
* @throws \BadMethodCallException Not used for OneToManyPersister
*/
protected function getUpdateRowSQL(PersistentCollection $coll)
public function get(PersistentCollection $coll, $index)
{
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) {
$columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
}
return 'INSERT INTO ' . $joinTable . ' (' . implode(', ', $columns) . ')'
. ' VALUES (' . implode(', ', array_fill(0, count($columns), '?')) . ')';
}
/**
* {@inheritdoc}
*
* @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;
throw new \BadMethodCallException("Selecting a collection by index is not supported by this CollectionPersister.");
}
/**
@ -238,9 +105,7 @@ class ManyToManyPersister extends AbstractCollectionPersister
$columnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
$referencedName = $joinColumn['referencedColumnName'];
$conditions[] = 't.' . $columnName . ' = ?';
$params[] = ($class->containsForeignIdentifier)
? $id[$class->getFieldForColumn($referencedName)]
: $id[$class->fieldNames[$referencedName]];
$params[] = $id[$class->getFieldForColumn($referencedName)];
}
$joinTableName = $this->quoteStrategy->getJoinTableName($association, $class, $this->platform);
@ -327,6 +192,322 @@ class ManyToManyPersister extends AbstractCollectionPersister
return (bool) $this->conn->executeUpdate($sql, $params);
}
/**
* {@inheritDoc}
*/
public function loadCriteria(PersistentCollection $coll, Criteria $criteria)
{
$mapping = $coll->getMapping();
$owner = $coll->getOwner();
$ownerMetadata = $this->em->getClassMetadata(get_class($owner));
$whereClauses = $params = array();
foreach ($mapping['relationToSourceKeyColumns'] as $key => $value) {
$whereClauses[] = sprintf('t.%s = ?', $key);
$params[] = $ownerMetadata->getFieldValue($owner, $value);
}
$parameters = $this->expandCriteriaParameters($criteria);
foreach ($parameters as $parameter) {
list($name, $value) = $parameter;
$whereClauses[] = sprintf('te.%s = ?', $name);
$params[] = $value;
}
$mapping = $coll->getMapping();
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
$tableName = $this->quoteStrategy->getTableName($targetClass, $this->platform);
$joinTable = $this->quoteStrategy->getJoinTableName($mapping, $ownerMetadata, $this->platform);
$onConditions = $this->getOnConditionSQL($mapping);
$rsm = new Query\ResultSetMappingBuilder($this->em);
$rsm->addRootEntityFromClassMetadata($mapping['targetEntity'], 'te');
$sql = 'SELECT ' . $rsm->generateSelectClause()
. ' FROM ' . $tableName . ' te'
. ' JOIN ' . $joinTable . ' t ON'
. implode(' AND ', $onConditions)
. ' WHERE ' . implode(' AND ', $whereClauses);
$stmt = $this->conn->executeQuery($sql, $params);
return $this
->em
->newHydrator(Query::HYDRATE_OBJECT)
->hydrateAll($stmt, $rsm);
}
/**
* Generates the filter SQL for a given mapping.
*
* This method is not used for actually grabbing the related entities
* but when the extra-lazy collection methods are called on a filtered
* association. This is why besides the many to many table we also
* have to join in the actual entities table leading to additional
* JOIN.
*
* @param array $mapping Array containing mapping information.
*
* @return string[] ordered tuple:
* - JOIN condition to add to the SQL
* - WHERE condition to add to the SQL
*/
public function getFilterSql($mapping)
{
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
$rootClass = $this->em->getClassMetadata($targetClass->rootEntityName);
$filterSql = $this->generateFilterConditionSQL($rootClass, 'te');
if ('' === $filterSql) {
return array('', '');
}
// A join is needed if there is filtering on the target entity
$tableName = $this->quoteStrategy->getTableName($rootClass, $this->platform);
$joinSql = ' JOIN ' . $tableName . ' te'
. ' ON' . implode(' AND ', $this->getOnConditionSQL($mapping));
return array($joinSql, $filterSql);
}
/**
* Generates the filter SQL for a given entity and table alias.
*
* @param ClassMetadata $targetEntity Metadata of the target entity.
* @param string $targetTableAlias The table alias of the joined/selected table.
*
* @return string The SQL query part to add to a query.
*/
protected function generateFilterConditionSQL(ClassMetadata $targetEntity, $targetTableAlias)
{
$filterClauses = array();
foreach ($this->em->getFilters()->getEnabledFilters() as $filter) {
if ($filterExpr = $filter->addFilterConstraint($targetEntity, $targetTableAlias)) {
$filterClauses[] = '(' . $filterExpr . ')';
}
}
return $filterClauses
? '(' . implode(' AND ', $filterClauses) . ')'
: '';
}
/**
* Generate ON condition
*
* @param array $mapping
*
* @return array
*/
protected function getOnConditionSQL($mapping)
{
$association = $mapping;
if ( ! $mapping['isOwningSide']) {
$association = $this
->em
->getClassMetadata($mapping['targetEntity'])
->associationMappings[$mapping['mappedBy']];
}
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
$joinColumns = $mapping['isOwningSide']
? $association['joinTable']['inverseJoinColumns']
: $association['joinTable']['joinColumns'];
$conditions = array();
foreach ($joinColumns as $joinColumn) {
$joinColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform);
$refColumnName = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform);
$conditions[] = ' t.' . $joinColumnName . ' = ' . 'te.' . $refColumnName;
}
return $conditions;
}
/**
* {@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;
}
/**
* Gets the SQL statement used for deleting a row from the collection.
*
* @param \Doctrine\ORM\PersistentCollection $coll
*
* @return string
*/
protected function getDeleteRowSQL(PersistentCollection $coll)
{
$mapping = $coll->getMapping();
$class = $this->em->getClassMetadata($mapping['sourceEntity']);
$columns = array();
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 ' . $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform)
. ' 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()));
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 ' . $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform)
. ' (' . 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
@ -377,9 +558,7 @@ class ManyToManyPersister extends AbstractCollectionPersister
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]]];
$params[] = $id[$targetEntity->getFieldForColumn($mapping[$relationMode][$joinTableColumn])];
} elseif (!$joinNeeded) {
$whereClauses[] = 't.' . $joinTableColumn . ' = ?';
$params[] = $key;
@ -432,17 +611,13 @@ class ManyToManyPersister extends AbstractCollectionPersister
$whereClauses[] = ($addFilters ? 't.' : '') . $joinTableColumn . ' = ?';
if (isset($mapping['relationToTargetKeyColumns'][$joinTableColumn])) {
$params[] = ($targetClass->containsForeignIdentifier)
? $targetId[$targetClass->getFieldForColumn($mapping['relationToTargetKeyColumns'][$joinTableColumn])]
: $targetId[$targetClass->fieldNames[$mapping['relationToTargetKeyColumns'][$joinTableColumn]]];
$params[] = $targetId[$targetClass->getFieldForColumn($mapping['relationToTargetKeyColumns'][$joinTableColumn])];
continue;
}
// relationToSourceKeyColumns
$params[] = ($sourceClass->containsForeignIdentifier)
? $sourceId[$sourceClass->getFieldForColumn($mapping['relationToSourceKeyColumns'][$joinTableColumn])]
: $sourceId[$sourceClass->fieldNames[$mapping['relationToSourceKeyColumns'][$joinTableColumn]]];
$params[] = $sourceId[$sourceClass->getFieldForColumn($mapping['relationToSourceKeyColumns'][$joinTableColumn])];
}
if ($addFilters) {
@ -459,138 +634,6 @@ class ManyToManyPersister extends AbstractCollectionPersister
return array($quotedJoinTable, $whereClauses, $params);
}
/**
* Generates the filter SQL for a given mapping.
*
* This method is not used for actually grabbing the related entities
* but when the extra-lazy collection methods are called on a filtered
* association. This is why besides the many to many table we also
* have to join in the actual entities table leading to additional
* JOIN.
*
* @param array $mapping Array containing mapping information.
*
* @return string The SQL query part to add to a query.
*/
public function getFilterSql($mapping)
{
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
$rootClass = $this->em->getClassMetadata($targetClass->rootEntityName);
$filterSql = $this->generateFilterConditionSQL($rootClass, 'te');
if ('' === $filterSql) {
return array('', '');
}
// A join is needed if there is filtering on the target entity
$tableName = $this->quoteStrategy->getTableName($rootClass, $this->platform);
$joinSql = ' JOIN ' . $tableName . ' te' . ' ON';
$onConditions = $this->getOnConditionSQL($mapping);
$joinSql .= implode(' AND ', $onConditions);
return array($joinSql, $filterSql);
}
/**
* Generates the filter SQL for a given entity and table alias.
*
* @param ClassMetadata $targetEntity Metadata of the target entity.
* @param string $targetTableAlias The table alias of the joined/selected table.
*
* @return string The SQL query part to add to a query.
*/
protected function generateFilterConditionSQL(ClassMetadata $targetEntity, $targetTableAlias)
{
$filterClauses = array();
foreach ($this->em->getFilters()->getEnabledFilters() as $filter) {
if ($filterExpr = $filter->addFilterConstraint($targetEntity, $targetTableAlias)) {
$filterClauses[] = '(' . $filterExpr . ')';
}
}
$sql = implode(' AND ', $filterClauses);
return $sql ? '(' . $sql . ')' : '';
}
/**
* Generate ON condition
*
* @param array $mapping
*
* @return array
*/
protected function getOnConditionSQL($mapping)
{
$association = $mapping;
if ( ! $mapping['isOwningSide']) {
$class = $this->em->getClassMetadata($mapping['targetEntity']);
$association = $class->associationMappings[$mapping['mappedBy']];
}
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
$joinColumns = $mapping['isOwningSide']
? $association['joinTable']['inverseJoinColumns']
: $association['joinTable']['joinColumns'];
$conditions = array();
foreach ($joinColumns as $joinColumn) {
$joinColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform);
$refColumnName = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform);
$conditions[] = ' t.' . $joinColumnName . ' = ' . 'te.' . $refColumnName;
}
return $conditions;
}
/**
* {@inheritDoc}
*/
public function loadCriteria(PersistentCollection $coll, Criteria $criteria)
{
$mapping = $coll->getMapping();
$owner = $coll->getOwner();
$ownerMetadata = $this->em->getClassMetadata(get_class($owner));
$whereClauses = $params = array();
foreach ($mapping['relationToSourceKeyColumns'] as $key => $value) {
$whereClauses[] = sprintf('t.%s = ?', $key);
$params[] = $ownerMetadata->getFieldValue($owner, $value);
}
$parameters = $this->expandCriteriaParameters($criteria);
foreach ($parameters as $parameter) {
list($name, $value) = $parameter;
$whereClauses[] = sprintf('te.%s = ?', $name);
$params[] = $value;
}
$mapping = $coll->getMapping();
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
$tableName = $this->quoteStrategy->getTableName($targetClass, $this->platform);
$joinTable = $this->quoteStrategy->getJoinTableName($mapping, $ownerMetadata, $this->platform);
$onConditions = $this->getOnConditionSQL($mapping);
$rsm = new Query\ResultSetMappingBuilder($this->em);
$rsm->addRootEntityFromClassMetadata($mapping['targetEntity'], 'te');
$sql = 'SELECT ' . $rsm->generateSelectClause() . ' FROM ' . $tableName . ' te'
. ' JOIN ' . $joinTable . ' t ON'
. implode(' AND ', $onConditions)
. ' WHERE ' . implode(' AND ', $whereClauses);
$stmt = $this->conn->executeQuery($sql, $params);
$hydrator = $this->em->newHydrator(Query::HYDRATE_OBJECT);
return $hydrator->hydrateAll($stmt, $rsm);
}
/**
* Expands Criteria Parameters by walking the expressions and grabbing all
* parameters and types from it.

View file

@ -33,6 +33,28 @@ use Doctrine\ORM\UnitOfWork;
*/
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}
*/
@ -45,86 +67,17 @@ class OneToManyPersister extends AbstractCollectionPersister
throw new \BadMethodCallException("Selecting a collection by index is only supported on indexed collections.");
}
return $persister->load(array($mapping['mappedBy'] => $coll->getOwner(), $mapping['indexBy'] => $index), null, null, array(), null, 1);
}
/**
* Generates the SQL UPDATE that updates a particular row's foreign
* key to null.
*
* @param \Doctrine\ORM\PersistentCollection $coll
*
* @return string
*
* @override
*/
protected function getDeleteRowSQL(PersistentCollection $coll)
{
$mapping = $coll->getMapping();
$class = $this->em->getClassMetadata($mapping['targetEntity']);
$tableName = $this->quoteStrategy->getTableName($class, $this->platform);
$idColumns = $class->getIdentifierColumnNames();
return 'DELETE FROM ' . $tableName
. ' WHERE ' . implode('= ? AND ', $idColumns) . ' = ?';
}
/**
* {@inheritdoc}
*/
protected function getDeleteRowSQLParameters(PersistentCollection $coll, $element)
{
return array_values($this->uow->getEntityIdentifier($element));
}
/**
* {@inheritdoc}
*
* @throws \BadMethodCallException Not used for OneToManyPersister.
*/
protected function getInsertRowSQL(PersistentCollection $coll)
{
throw new \BadMethodCallException("Insert Row SQL is not used for OneToManyPersister");
}
/**
* {@inheritdoc}
*
* @throws \BadMethodCallException Not used for OneToManyPersister.
*/
protected function getInsertRowSQLParameters(PersistentCollection $coll, $element)
{
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");
return $persister->load(
array(
$mapping['mappedBy'] => $coll->getOwner(),
$mapping['indexBy'] => $index
),
null,
null,
array(),
null,
1
);
}
/**
@ -178,14 +131,7 @@ class OneToManyPersister extends AbstractCollectionPersister
*/
public function contains(PersistentCollection $coll, $element)
{
$entityState = $this->uow->getEntityState($element, UnitOfWork::STATE_NEW);
if ($entityState === UnitOfWork::STATE_NEW) {
return false;
}
// Entity is scheduled for inclusion
if ($entityState === UnitOfWork::STATE_MANAGED && $this->uow->isScheduledForInsert($element)) {
if ( ! $this->isValidEntityState($element)) {
return false;
}
@ -205,15 +151,7 @@ class OneToManyPersister extends AbstractCollectionPersister
*/
public function removeElement(PersistentCollection $coll, $element)
{
$entityState = $this->uow->getEntityState($element, UnitOfWork::STATE_NEW);
if ($entityState === UnitOfWork::STATE_NEW) {
return false;
}
// If Entity is scheduled for inclusion, it is not in this collection.
// We can assure that because it would have return true before on array check
if ($entityState === UnitOfWork::STATE_MANAGED && $this->uow->isScheduledForInsert($element)) {
if ( ! $this->isValidEntityState($element)) {
return false;
}
@ -222,4 +160,36 @@ class OneToManyPersister extends AbstractCollectionPersister
return $persister->delete($element);
}
/**
* {@inheritdoc}
*/
public function loadCriteria(PersistentCollection $collection, Criteria $criteria)
{
throw new \BadMethodCallException("Filtering a collection by Criteria is not supported by this CollectionPersister.");
}
/**
* Check if entity is in a valid state for operations.
*
* @param object $entity
*
* @return bool
*/
private function isValidEntityState($entity)
{
$entityState = $this->uow->getEntityState($entity, UnitOfWork::STATE_NEW);
if ($entityState === UnitOfWork::STATE_NEW) {
return false;
}
// If Entity is scheduled for inclusion, it is not in this collection.
// We can assure that because it would have return true before on array check
if ($entityState === UnitOfWork::STATE_MANAGED && $this->uow->isScheduledForInsert($entity)) {
return false;
}
return true;
}
}

View file

@ -57,7 +57,7 @@ class FilterCollection
/**
* Instances of enabled filters.
*
* @var array
* @var \Doctrine\ORM\Query\Filter\SQLFilter[]
*/
private $enabledFilters = array();
@ -85,7 +85,7 @@ class FilterCollection
/**
* Gets all the enabled filters.
*
* @return array The enabled filters.
* @return \Doctrine\ORM\Query\Filter\SQLFilter[] The enabled filters.
*/
public function getEnabledFilters()
{

View file

@ -49,8 +49,6 @@ abstract class AbstractCollectionPersisterTest extends OrmTestCase
protected $collectionPersisterMockMethods = array(
'delete',
'update',
'deleteRows',
'insertRows',
'count',
'slice',
'contains',
@ -154,36 +152,6 @@ abstract class AbstractCollectionPersisterTest extends OrmTestCase
$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()
{
$entity = new State("Foo");