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