2008-09-12 13:39:43 +04:00
|
|
|
<?php
|
2009-02-05 20:34:44 +03:00
|
|
|
/*
|
|
|
|
* 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
|
2012-05-26 16:37:00 +04:00
|
|
|
* and is licensed under the MIT license. For more information, see
|
2009-02-05 20:34:44 +03:00
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
2008-09-12 13:39:43 +04:00
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
namespace Doctrine\ORM\Persisters;
|
|
|
|
|
2012-10-12 15:53:20 +04:00
|
|
|
use Doctrine\ORM\PersistentCollection;
|
|
|
|
use Doctrine\ORM\UnitOfWork;
|
2009-02-02 14:55:50 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Persister for one-to-many collections.
|
2010-05-05 15:12:38 +04:00
|
|
|
*
|
2011-11-29 20:29:17 +04:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
2011-12-06 01:05:42 +04:00
|
|
|
* @author Alexander <iam.asm89@gmail.com>
|
2011-11-29 20:29:17 +04:00
|
|
|
* @since 2.0
|
2009-02-02 14:55:50 +03:00
|
|
|
*/
|
|
|
|
class OneToManyPersister extends AbstractCollectionPersister
|
2008-09-12 13:39:43 +04:00
|
|
|
{
|
2009-02-04 19:35:36 +03:00
|
|
|
/**
|
2009-07-21 19:53:58 +04:00
|
|
|
* Generates the SQL UPDATE that updates a particular row's foreign
|
|
|
|
* key to null.
|
2009-02-04 19:35:36 +03:00
|
|
|
*
|
2012-07-22 22:26:00 +04:00
|
|
|
* @param \Doctrine\ORM\PersistentCollection $coll
|
2009-07-21 19:53:58 +04:00
|
|
|
* @return string
|
2009-02-04 19:35:36 +03:00
|
|
|
* @override
|
|
|
|
*/
|
2012-07-22 22:26:00 +04:00
|
|
|
protected function getDeleteRowSQL(PersistentCollection $coll)
|
2009-02-02 14:55:50 +03:00
|
|
|
{
|
2012-07-28 05:20:49 +04:00
|
|
|
$mapping = $coll->getMapping();
|
|
|
|
$class = $this->em->getClassMetadata($mapping['targetEntity']);
|
|
|
|
$tableName = $this->quoteStrategy->getTableName($class, $this->platform);
|
|
|
|
$idColumns = $class->getIdentifierColumnNames();
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-07-28 05:20:49 +04:00
|
|
|
return 'DELETE FROM ' . $tableName
|
|
|
|
. ' WHERE ' . implode('= ? AND ', $idColumns) . ' = ?';
|
2011-11-29 20:29:17 +04:00
|
|
|
}
|
2009-02-02 14:55:50 +03:00
|
|
|
|
2011-11-29 20:29:17 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-07-22 22:26:00 +04:00
|
|
|
protected function getDeleteRowSQLParameters(PersistentCollection $coll, $element)
|
2011-11-29 20:29:17 +04:00
|
|
|
{
|
2012-07-22 22:26:00 +04:00
|
|
|
return array_values($this->uow->getEntityIdentifier($element));
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
|
|
|
|
2012-07-22 22:26:00 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
* @throws \BadMethodCallException Not used for OneToManyPersister
|
|
|
|
*/
|
|
|
|
protected function getInsertRowSQL(PersistentCollection $coll)
|
2009-02-02 14:55:50 +03:00
|
|
|
{
|
2012-07-22 22:26:00 +04:00
|
|
|
throw new \BadMethodCallException("Insert Row SQL is not used for OneToManyPersister");
|
2009-02-02 14:55:50 +03:00
|
|
|
}
|
|
|
|
|
2011-11-29 20:29:17 +04:00
|
|
|
/**
|
2012-07-22 22:26:00 +04:00
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @throws \BadMethodCallException Not used for OneToManyPersister
|
2011-11-29 20:29:17 +04:00
|
|
|
*/
|
2012-07-22 22:26:00 +04:00
|
|
|
protected function getInsertRowSQLParameters(PersistentCollection $coll, $element)
|
2009-02-02 14:55:50 +03:00
|
|
|
{
|
2012-07-22 22:26:00 +04:00
|
|
|
throw new \BadMethodCallException("Insert Row SQL is not used for OneToManyPersister");
|
2009-02-02 14:55:50 +03:00
|
|
|
}
|
2010-05-05 15:12:38 +04:00
|
|
|
|
2009-07-21 19:53:58 +04:00
|
|
|
/**
|
2012-07-22 22:26:00 +04:00
|
|
|
* {@inheritdoc}
|
2009-07-21 19:53:58 +04:00
|
|
|
*
|
2012-07-22 22:26:00 +04:00
|
|
|
* @throws \BadMethodCallException Not used for OneToManyPersister
|
2009-07-21 19:53:58 +04:00
|
|
|
*/
|
2012-07-22 22:26:00 +04:00
|
|
|
protected function getUpdateRowSQL(PersistentCollection $coll)
|
2009-07-21 19:53:58 +04:00
|
|
|
{
|
2012-07-22 22:26:00 +04:00
|
|
|
throw new \BadMethodCallException("Update Row SQL is not used for OneToManyPersister");
|
|
|
|
}
|
2010-05-05 15:12:38 +04:00
|
|
|
|
2012-07-22 22:26:00 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @throws \BadMethodCallException Not used for OneToManyPersister
|
|
|
|
*/
|
|
|
|
protected function getDeleteSQL(PersistentCollection $coll)
|
|
|
|
{
|
|
|
|
throw new \BadMethodCallException("Update Row SQL is not used for OneToManyPersister");
|
2009-07-21 19:53:58 +04:00
|
|
|
}
|
2010-05-05 15:12:38 +04:00
|
|
|
|
2009-07-21 19:53:58 +04:00
|
|
|
/**
|
2012-07-22 22:26:00 +04:00
|
|
|
* {@inheritdoc}
|
2009-07-21 19:53:58 +04:00
|
|
|
*
|
2012-07-22 22:26:00 +04:00
|
|
|
* @throws \BadMethodCallException Not used for OneToManyPersister
|
2009-07-21 19:53:58 +04:00
|
|
|
*/
|
2012-07-22 22:26:00 +04:00
|
|
|
protected function getDeleteSQLParameters(PersistentCollection $coll)
|
|
|
|
{
|
|
|
|
throw new \BadMethodCallException("Update Row SQL is not used for OneToManyPersister");
|
|
|
|
}
|
2010-05-05 15:12:38 +04:00
|
|
|
|
2010-12-04 21:44:10 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function count(PersistentCollection $coll)
|
|
|
|
{
|
2011-11-29 20:29:17 +04:00
|
|
|
$mapping = $coll->getMapping();
|
2012-07-22 22:26:00 +04:00
|
|
|
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
|
|
|
|
$sourceClass = $this->em->getClassMetadata($mapping['sourceEntity']);
|
|
|
|
$id = $this->em->getUnitOfWork()->getEntityIdentifier($coll->getOwner());
|
2011-07-28 13:01:52 +04:00
|
|
|
|
2011-11-29 20:29:17 +04:00
|
|
|
$whereClauses = array();
|
|
|
|
$params = array();
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-07-28 05:20:49 +04:00
|
|
|
$joinColumns = $targetClass->associationMappings[$mapping['mappedBy']]['joinColumns'];
|
|
|
|
foreach ($joinColumns as $joinColumn) {
|
2011-11-29 20:29:17 +04:00
|
|
|
$whereClauses[] = $joinColumn['name'] . ' = ?';
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-11-29 20:29:17 +04:00
|
|
|
$params[] = ($targetClass->containsForeignIdentifier)
|
|
|
|
? $id[$sourceClass->getFieldForColumn($joinColumn['referencedColumnName'])]
|
|
|
|
: $id[$sourceClass->fieldNames[$joinColumn['referencedColumnName']]];
|
2010-12-04 21:44:10 +03:00
|
|
|
}
|
|
|
|
|
2012-07-22 22:26:00 +04:00
|
|
|
$filterTargetClass = $this->em->getClassMetadata($targetClass->rootEntityName);
|
|
|
|
foreach ($this->em->getFilters()->getEnabledFilters() as $filter) {
|
2011-12-22 23:50:57 +04:00
|
|
|
if ($filterExpr = $filter->addFilterConstraint($filterTargetClass, 't')) {
|
2011-12-06 00:53:34 +04:00
|
|
|
$whereClauses[] = '(' . $filterExpr . ')';
|
2011-08-16 18:24:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-29 20:29:17 +04:00
|
|
|
$sql = 'SELECT count(*)'
|
2012-06-12 03:14:17 +04:00
|
|
|
. ' FROM ' . $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' t'
|
2011-11-29 20:29:17 +04:00
|
|
|
. ' WHERE ' . implode(' AND ', $whereClauses);
|
2011-12-06 00:53:34 +04:00
|
|
|
|
2012-07-22 22:26:00 +04:00
|
|
|
return $this->conn->fetchColumn($sql, $params);
|
2010-12-04 21:44:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-22 22:26:00 +04:00
|
|
|
* @param \Doctrine\ORM\PersistentCollection $coll
|
2010-12-04 21:44:10 +03:00
|
|
|
* @param int $offset
|
|
|
|
* @param int $length
|
|
|
|
* @return \Doctrine\Common\Collections\ArrayCollection
|
|
|
|
*/
|
|
|
|
public function slice(PersistentCollection $coll, $offset, $length = null)
|
|
|
|
{
|
2011-11-29 20:29:17 +04:00
|
|
|
$mapping = $coll->getMapping();
|
2012-07-22 22:26:00 +04:00
|
|
|
$uow = $this->em->getUnitOfWork();
|
2011-11-29 20:29:17 +04:00
|
|
|
$persister = $uow->getEntityPersister($mapping['targetEntity']);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-11-29 20:29:17 +04:00
|
|
|
return $persister->getOneToManyCollection($mapping, $coll->getOwner(), $offset, $length);
|
2010-12-04 21:44:10 +03:00
|
|
|
}
|
2010-12-29 14:27:14 +03:00
|
|
|
|
|
|
|
/**
|
2012-07-22 22:26:00 +04:00
|
|
|
* @param \Doctrine\ORM\PersistentCollection $coll
|
2010-12-29 14:27:14 +03:00
|
|
|
* @param object $element
|
2011-11-29 20:29:17 +04:00
|
|
|
* @return boolean
|
2010-12-29 14:27:14 +03:00
|
|
|
*/
|
|
|
|
public function contains(PersistentCollection $coll, $element)
|
|
|
|
{
|
|
|
|
$mapping = $coll->getMapping();
|
2012-07-22 22:26:00 +04:00
|
|
|
$uow = $this->em->getUnitOfWork();
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-12-29 14:27:14 +03:00
|
|
|
// shortcut for new entities
|
2012-01-18 10:04:25 +04:00
|
|
|
$entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW);
|
|
|
|
|
|
|
|
if ($entityState === UnitOfWork::STATE_NEW) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entity is scheduled for inclusion
|
|
|
|
if ($entityState === UnitOfWork::STATE_MANAGED && $uow->isScheduledForInsert($element)) {
|
2010-12-29 14:27:14 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-29 20:29:17 +04:00
|
|
|
$persister = $uow->getEntityPersister($mapping['targetEntity']);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
|
|
|
// only works with single id identifier entities. Will throw an
|
|
|
|
// exception in Entity Persisters if that is not the case for the
|
2011-11-29 20:29:17 +04:00
|
|
|
// 'mappedBy' field.
|
2012-01-18 10:04:25 +04:00
|
|
|
$id = current( $uow->getEntityIdentifier($coll->getOwner()));
|
2010-12-29 14:27:14 +03:00
|
|
|
|
2011-11-29 20:29:17 +04:00
|
|
|
return $persister->exists($element, array($mapping['mappedBy'] => $id));
|
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-11-29 20:29:17 +04:00
|
|
|
/**
|
2012-07-22 22:26:00 +04:00
|
|
|
* @param \Doctrine\ORM\PersistentCollection $coll
|
2011-11-29 20:29:17 +04:00
|
|
|
* @param object $element
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function removeElement(PersistentCollection $coll, $element)
|
|
|
|
{
|
2012-07-22 22:26:00 +04:00
|
|
|
$uow = $this->em->getUnitOfWork();
|
2011-11-29 20:29:17 +04:00
|
|
|
|
|
|
|
// shortcut for new entities
|
2012-01-18 10:04:25 +04:00
|
|
|
$entityState = $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 && $uow->isScheduledForInsert($element)) {
|
2011-11-29 20:29:17 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$mapping = $coll->getMapping();
|
2012-07-22 22:26:00 +04:00
|
|
|
$class = $this->em->getClassMetadata($mapping['targetEntity']);
|
2012-06-12 03:14:17 +04:00
|
|
|
$sql = 'DELETE FROM ' . $this->quoteStrategy->getTableName($class, $this->platform)
|
2011-11-29 20:29:17 +04:00
|
|
|
. ' WHERE ' . implode('= ? AND ', $class->getIdentifierColumnNames()) . ' = ?';
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-07-22 22:26:00 +04:00
|
|
|
return (bool) $this->conn->executeUpdate($sql, $this->getDeleteRowSQLParameters($coll, $element));
|
2010-12-29 14:27:14 +03:00
|
|
|
}
|
2011-07-28 13:01:52 +04:00
|
|
|
}
|