2008-09-12 13:44:51 +04:00
|
|
|
<?php
|
2009-05-03 15:49:48 +04: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-05-03 15:49:48 +04:00
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
2008-09-12 13:44:51 +04:00
|
|
|
|
2009-02-20 08:46:20 +03:00
|
|
|
namespace Doctrine\ORM\Persisters;
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
2012-06-19 02:01:03 +04:00
|
|
|
use Doctrine\Common\Collections\Criteria;
|
2009-11-21 21:52:02 +03:00
|
|
|
|
2009-05-03 15:49:48 +04:00
|
|
|
/**
|
|
|
|
* Persister for entities that participate in a hierarchy mapped with the
|
|
|
|
* SINGLE_TABLE strategy.
|
|
|
|
*
|
2010-03-29 17:20:41 +04:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2011-03-13 02:23:46 +03:00
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
2011-12-06 01:05:42 +04:00
|
|
|
* @author Alexander <iam.asm89@gmail.com>
|
2010-03-29 17:20:41 +04:00
|
|
|
* @since 2.0
|
|
|
|
* @link http://martinfowler.com/eaaCatalog/singleTableInheritance.html
|
2009-05-03 15:49:48 +04:00
|
|
|
*/
|
2010-03-29 17:20:41 +04:00
|
|
|
class SingleTablePersister extends AbstractEntityInheritancePersister
|
2009-02-20 08:46:20 +03:00
|
|
|
{
|
2012-07-28 05:29:52 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-07-22 20:42:43 +04:00
|
|
|
protected function getDiscriminatorColumnTableName()
|
2009-04-09 22:12:48 +04:00
|
|
|
{
|
2012-07-22 20:42:43 +04:00
|
|
|
return $this->class->getTableName();
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2010-03-29 17:20:41 +04:00
|
|
|
|
2012-07-28 05:29:52 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-07-25 05:33:23 +04:00
|
|
|
protected function getSelectColumnsSQL()
|
2009-11-21 21:52:02 +03:00
|
|
|
{
|
2012-07-22 20:42:43 +04:00
|
|
|
if ($this->selectColumnListSql !== null) {
|
|
|
|
return $this->selectColumnListSql;
|
2011-10-10 19:52:37 +04:00
|
|
|
}
|
|
|
|
|
2012-07-28 05:29:52 +04:00
|
|
|
$columnList[] = parent::getSelectColumnsSQL();
|
2010-04-26 15:02:30 +04:00
|
|
|
|
2012-07-22 20:42:43 +04:00
|
|
|
$rootClass = $this->em->getClassMetadata($this->class->rootEntityName);
|
|
|
|
$tableAlias = $this->getSQLTableAlias($rootClass->name);
|
2012-01-13 08:58:08 +04:00
|
|
|
|
|
|
|
// Append discriminator column
|
2012-07-28 05:29:52 +04:00
|
|
|
$discrColumn = $this->class->discriminatorColumn['name'];
|
|
|
|
$columnList[] = $tableAlias . '.' . $discrColumn;
|
2012-01-13 08:58:08 +04:00
|
|
|
|
2012-07-22 20:42:43 +04:00
|
|
|
$resultColumnName = $this->platform->getSQLResultCasing($discrColumn);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-07-22 20:42:43 +04:00
|
|
|
$this->rsm->setDiscriminatorColumn('r', $resultColumnName);
|
|
|
|
$this->rsm->addMetaResult('r', $resultColumnName, $discrColumn);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-04-26 15:02:30 +04:00
|
|
|
// Append subclass columns
|
2012-07-22 20:42:43 +04:00
|
|
|
foreach ($this->class->subClasses as $subClassName) {
|
|
|
|
$subClass = $this->em->getClassMetadata($subClassName);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-04-26 15:02:30 +04:00
|
|
|
// Regular columns
|
2009-11-21 21:52:02 +03:00
|
|
|
foreach ($subClass->fieldMappings as $fieldName => $mapping) {
|
2012-07-28 05:29:52 +04:00
|
|
|
if (isset($mapping['inherited'])) {
|
|
|
|
continue;
|
2009-11-21 21:52:02 +03:00
|
|
|
}
|
2012-07-28 05:29:52 +04:00
|
|
|
|
|
|
|
$columnList[] = $this->getSelectColumnSQL($fieldName, $subClass);
|
2009-11-21 21:52:02 +03:00
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-04-26 15:02:30 +04:00
|
|
|
// Foreign key columns
|
2009-11-21 21:52:02 +03:00
|
|
|
foreach ($subClass->associationMappings as $assoc) {
|
2012-07-28 05:29:52 +04:00
|
|
|
if ( ! $assoc['isOwningSide']
|
|
|
|
|| ! ($assoc['type'] & ClassMetadata::TO_ONE)
|
|
|
|
|| isset($assoc['inherited'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) {
|
|
|
|
$className = isset($assoc['inherited']) ? $assoc['inherited'] : $this->class->name;
|
|
|
|
$columnList[] = $this->getSelectJoinColumnSQL($tableAlias, $srcColumn, $className);
|
2009-11-21 21:52:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-28 05:29:52 +04:00
|
|
|
$this->selectColumnListSql = implode(', ', $columnList);
|
|
|
|
|
2012-07-22 20:42:43 +04:00
|
|
|
return $this->selectColumnListSql;
|
2009-11-21 21:52:02 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-07-25 05:33:23 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-07-22 20:42:43 +04:00
|
|
|
protected function getInsertColumnList()
|
2009-12-17 16:37:47 +03:00
|
|
|
{
|
2012-07-22 20:42:43 +04:00
|
|
|
$columns = parent::getInsertColumnList();
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-12-17 16:37:47 +03:00
|
|
|
// Add discriminator column to the INSERT SQL
|
2012-07-22 20:42:43 +04:00
|
|
|
$columns[] = $this->class->discriminatorColumn['name'];
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-12-17 16:37:47 +03:00
|
|
|
return $columns;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-07-25 05:33:23 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-07-22 20:42:43 +04:00
|
|
|
protected function getSQLTableAlias($className, $assocName = '')
|
2010-03-05 19:35:00 +03:00
|
|
|
{
|
2012-07-22 20:42:43 +04:00
|
|
|
return parent::getSQLTableAlias($this->class->rootEntityName, $assocName);
|
2010-04-26 15:02:30 +04:00
|
|
|
}
|
|
|
|
|
2012-07-25 05:33:23 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-07-22 20:42:43 +04:00
|
|
|
protected function getSelectConditionSQL(array $criteria, $assoc = null)
|
2010-04-26 15:02:30 +04:00
|
|
|
{
|
2012-07-22 20:42:43 +04:00
|
|
|
$conditionSql = parent::getSelectConditionSQL($criteria, $assoc);
|
2010-04-26 15:02:30 +04:00
|
|
|
|
2012-06-19 02:01:03 +04:00
|
|
|
if ($conditionSql) {
|
|
|
|
$conditionSql .= ' AND ';
|
|
|
|
}
|
|
|
|
|
2012-07-22 20:42:43 +04:00
|
|
|
return $conditionSql . $this->getSelectConditionDiscriminatorValueSQL();
|
2012-06-19 02:01:03 +04:00
|
|
|
}
|
|
|
|
|
2012-07-25 05:33:23 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-07-22 20:42:43 +04:00
|
|
|
protected function getSelectConditionCriteriaSQL(Criteria $criteria)
|
2012-06-19 02:01:03 +04:00
|
|
|
{
|
2012-07-22 20:42:43 +04:00
|
|
|
$conditionSql = parent::getSelectConditionCriteriaSQL($criteria);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-06-19 02:01:03 +04:00
|
|
|
if ($conditionSql) {
|
|
|
|
$conditionSql .= ' AND ';
|
|
|
|
}
|
|
|
|
|
2012-07-22 20:42:43 +04:00
|
|
|
return $conditionSql . $this->getSelectConditionDiscriminatorValueSQL();
|
2012-06-19 02:01:03 +04:00
|
|
|
}
|
|
|
|
|
2012-12-03 14:15:43 +04:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-07-22 20:42:43 +04:00
|
|
|
protected function getSelectConditionDiscriminatorValueSQL()
|
2012-06-19 02:01:03 +04:00
|
|
|
{
|
2010-07-04 15:34:14 +04:00
|
|
|
$values = array();
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-07-22 20:42:43 +04:00
|
|
|
if ($this->class->discriminatorValue !== null) { // discriminators can be 0
|
|
|
|
$values[] = $this->conn->quote($this->class->discriminatorValue);
|
2010-07-04 15:34:14 +04:00
|
|
|
}
|
|
|
|
|
2012-07-22 20:42:43 +04:00
|
|
|
$discrValues = array_flip($this->class->discriminatorMap);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-07-22 20:42:43 +04:00
|
|
|
foreach ($this->class->subClasses as $subclassName) {
|
|
|
|
$values[] = $this->conn->quote($discrValues[$subclassName]);
|
2010-03-05 19:35:00 +03:00
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-07-28 05:29:52 +04:00
|
|
|
$values = implode(', ', $values);
|
|
|
|
$discColumn = $this->class->discriminatorColumn['name'];
|
|
|
|
$tableAlias = $this->getSQLTableAlias($this->class->name);
|
|
|
|
|
|
|
|
return $tableAlias . '.' . $discColumn . ' IN (' . $values . ')';
|
2009-11-21 21:52:02 +03:00
|
|
|
}
|
2011-12-05 21:56:44 +04:00
|
|
|
|
2012-07-25 05:33:23 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2011-12-05 21:56:44 +04:00
|
|
|
protected function generateFilterConditionSQL(ClassMetadata $targetEntity, $targetTableAlias)
|
|
|
|
{
|
|
|
|
// Ensure that the filters are applied to the root entity of the inheritance tree
|
2012-07-22 20:42:43 +04:00
|
|
|
$targetEntity = $this->em->getClassMetadata($targetEntity->rootEntityName);
|
2011-12-11 22:29:36 +04:00
|
|
|
// we dont care about the $targetTableAlias, in a STI there is only one table.
|
2011-12-05 21:56:44 +04:00
|
|
|
|
2011-12-11 22:29:36 +04:00
|
|
|
return parent::generateFilterConditionSQL($targetEntity, $targetTableAlias);
|
2011-12-05 21:56:44 +04:00
|
|
|
}
|
2011-11-23 03:15:23 +04:00
|
|
|
}
|