2009-02-02 14:55:50 +03:00
|
|
|
<?php
|
2009-02-05 20:34:44 +03:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.doctrine-project.org>.
|
2009-02-02 14:55:50 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\ORM\Persisters;
|
|
|
|
|
2009-02-06 20:16:39 +03:00
|
|
|
use Doctrine\ORM\PersistentCollection;
|
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
/**
|
|
|
|
* Persister for many-to-many collections.
|
|
|
|
*
|
2009-05-13 19:19:27 +04:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2009-02-06 20:16:39 +03:00
|
|
|
* @since 2.0
|
2009-02-02 14:55:50 +03:00
|
|
|
*/
|
|
|
|
class ManyToManyPersister extends AbstractCollectionPersister
|
|
|
|
{
|
|
|
|
/**
|
2009-02-06 20:16:39 +03:00
|
|
|
* {@inheritdoc}
|
2009-02-02 14:55:50 +03:00
|
|
|
*
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
protected function _getDeleteRowSql(PersistentCollection $coll)
|
|
|
|
{
|
2009-02-04 19:35:36 +03:00
|
|
|
$mapping = $coll->getMapping();
|
|
|
|
$joinTable = $mapping->getJoinTable();
|
2009-08-11 14:51:38 +04:00
|
|
|
$columns = $mapping->getJoinTableColumnNames();
|
2009-05-13 19:19:27 +04:00
|
|
|
return 'DELETE FROM ' . $joinTable['name'] . ' WHERE ' . implode(' = ? AND ', $columns) . ' = ?';
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-06 20:16:39 +03:00
|
|
|
* {@inheritdoc}
|
2009-02-04 19:35:36 +03:00
|
|
|
*
|
|
|
|
* @override
|
2009-11-21 21:52:02 +03:00
|
|
|
* @internal Order of the parameters must be the same as the order of the columns in
|
|
|
|
* _getDeleteRowSql.
|
2009-02-04 19:35:36 +03:00
|
|
|
*/
|
|
|
|
protected function _getDeleteRowSqlParameters(PersistentCollection $coll, $element)
|
|
|
|
{
|
2009-11-21 21:52:02 +03:00
|
|
|
return $this->_collectJoinTableColumnParameters($coll, $element);
|
2009-02-06 20:16:39 +03:00
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
|
2009-02-06 20:16:39 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
protected function _getUpdateRowSql(PersistentCollection $coll)
|
2009-02-07 20:02:13 +03:00
|
|
|
{}
|
2009-02-06 20:16:39 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @override
|
2009-11-21 21:52:02 +03:00
|
|
|
* @internal Order of the parameters must be the same as the order of the columns in
|
|
|
|
* _getInsertRowSql.
|
2009-02-06 20:16:39 +03:00
|
|
|
*/
|
|
|
|
protected function _getInsertRowSql(PersistentCollection $coll)
|
|
|
|
{
|
|
|
|
$mapping = $coll->getMapping();
|
|
|
|
$joinTable = $mapping->getJoinTable();
|
2009-11-21 21:52:02 +03:00
|
|
|
$columns = $mapping->joinTableColumns;
|
2009-05-13 19:19:27 +04:00
|
|
|
return 'INSERT INTO ' . $joinTable['name'] . ' (' . implode(', ', $columns) . ')'
|
|
|
|
. ' VALUES (' . implode(', ', array_fill(0, count($columns), '?')) . ')';
|
2009-02-06 20:16:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @override
|
2009-11-21 21:52:02 +03:00
|
|
|
* @internal Order of the parameters must be the same as the order of the columns in
|
|
|
|
* _getInsertRowSql.
|
2009-02-06 20:16:39 +03:00
|
|
|
*/
|
|
|
|
protected function _getInsertRowSqlParameters(PersistentCollection $coll, $element)
|
|
|
|
{
|
2009-11-21 21:52:02 +03:00
|
|
|
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 $coll
|
|
|
|
* @param $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) {
|
|
|
|
if (isset($mapping->relationToSourceKeyColumns[$joinTableColumn])) {
|
|
|
|
if ($isComposite) {
|
|
|
|
$params[] = $identifier1[$class1->fieldNames[$mapping->relationToSourceKeyColumns[$joinTableColumn]]];
|
|
|
|
} else {
|
|
|
|
$params[] = array_pop($identifier1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($isComposite) {
|
|
|
|
$params[] = $identifier2[$class2->fieldNames[$mapping->relationToTargetKeyColumns[$joinTableColumn]]];
|
|
|
|
} else {
|
|
|
|
$params[] = array_pop($identifier2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-06 20:16:39 +03:00
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
protected function _getDeleteSql(PersistentCollection $coll)
|
|
|
|
{
|
|
|
|
$mapping = $coll->getMapping();
|
|
|
|
$joinTable = $mapping->getJoinTable();
|
|
|
|
$whereClause = '';
|
2009-11-21 21:52:02 +03:00
|
|
|
foreach ($mapping->relationToSourceKeyColumns as $relationColumn => $srcColumn) {
|
2009-02-06 20:16:39 +03:00
|
|
|
if ($whereClause !== '') $whereClause .= ' AND ';
|
|
|
|
$whereClause .= "$relationColumn = ?";
|
|
|
|
}
|
2009-05-13 19:19:27 +04:00
|
|
|
return 'DELETE FROM ' . $joinTable['name'] . ' WHERE ' . $whereClause;
|
2009-02-06 20:16:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @override
|
2009-11-21 21:52:02 +03:00
|
|
|
* @internal Order of the parameters must be the same as the order of the columns in
|
|
|
|
* _getDeleteSql.
|
2009-02-06 20:16:39 +03:00
|
|
|
*/
|
|
|
|
protected function _getDeleteSqlParameters(PersistentCollection $coll)
|
|
|
|
{
|
2009-11-21 21:52:02 +03:00
|
|
|
$params = array();
|
|
|
|
$mapping = $coll->getMapping();
|
|
|
|
$identifier = $this->_uow->getEntityIdentifier($coll->getOwner());
|
|
|
|
if (count($mapping->relationToSourceKeyColumns) > 1) {
|
|
|
|
$sourceClass = $this->_em->getClassMetadata(get_class($mapping->getOwner()));
|
|
|
|
foreach ($mapping->relationToSourceKeyColumns as $relColumn => $srcColumn) {
|
|
|
|
$params[] = $identifier[$sourceClass->fieldNames[$srcColumn]];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$params[] = array_pop($identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $params;
|
2009-02-02 14:55:50 +03:00
|
|
|
}
|
2009-02-20 08:46:20 +03:00
|
|
|
}
|