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
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <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;
|
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
|
|
|
{
|
2010-03-29 17:20:41 +04:00
|
|
|
/** {@inheritdoc} */
|
|
|
|
protected function _getDiscriminatorColumnTableName()
|
2009-04-09 22:12:48 +04:00
|
|
|
{
|
2011-09-07 08:48:19 +04:00
|
|
|
return $this->_class->getTableName();
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2010-03-29 17:20:41 +04:00
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
2010-03-05 19:35:00 +03:00
|
|
|
protected function _getSelectColumnListSQL()
|
2009-11-21 21:52:02 +03:00
|
|
|
{
|
2011-10-10 19:52:37 +04:00
|
|
|
if ($this->_selectColumnListSql !== null) {
|
|
|
|
return $this->_selectColumnListSql;
|
|
|
|
}
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
$columnList = parent::_getSelectColumnListSQL();
|
2010-04-26 15:02:30 +04:00
|
|
|
|
2009-11-21 21:52:02 +03:00
|
|
|
// Append discriminator column
|
2010-03-05 19:35:00 +03:00
|
|
|
$discrColumn = $this->_class->discriminatorColumn['name'];
|
2011-09-07 08:48:19 +04:00
|
|
|
$columnList .= ', ' . $discrColumn;
|
|
|
|
|
|
|
|
$rootClass = $this->_em->getClassMetadata($this->_class->rootEntityName);
|
2010-04-26 15:02:30 +04:00
|
|
|
$tableAlias = $this->_getSQLTableAlias($rootClass->name);
|
2010-03-05 19:35:00 +03:00
|
|
|
$resultColumnName = $this->_platform->getSQLResultCasing($discrColumn);
|
2011-09-07 08:48:19 +04:00
|
|
|
|
2011-06-26 19:49:34 +04:00
|
|
|
$this->_rsm->setDiscriminatorColumn('r', $resultColumnName);
|
2011-03-06 23:26:54 +03:00
|
|
|
$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
|
2009-11-21 21:52:02 +03:00
|
|
|
foreach ($this->_class->subClasses as $subClassName) {
|
|
|
|
$subClass = $this->_em->getClassMetadata($subClassName);
|
2011-09-07 08:48: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) {
|
|
|
|
if ( ! isset($mapping['inherited'])) {
|
2010-03-05 19:35:00 +03:00
|
|
|
$columnList .= ', ' . $this->_getSelectColumnSQL($fieldName, $subClass);
|
2009-11-21 21:52:02 +03:00
|
|
|
}
|
|
|
|
}
|
2011-09-07 08:48: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) {
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE && ! isset($assoc['inherited'])) {
|
|
|
|
foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) {
|
2010-12-28 16:56:13 +03:00
|
|
|
if ($columnList != '') $columnList .= ', ';
|
2011-09-07 08:48:19 +04:00
|
|
|
|
|
|
|
$columnList .= $this->getSelectJoinColumnSQL(
|
|
|
|
$tableAlias,
|
|
|
|
$srcColumn,
|
2010-12-28 16:56:13 +03:00
|
|
|
isset($assoc['inherited']) ? $assoc['inherited'] : $this->_class->name
|
|
|
|
);
|
2009-11-21 21:52:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-10 19:52:37 +04:00
|
|
|
$this->_selectColumnListSql = $columnList;
|
|
|
|
return $this->_selectColumnListSql;
|
2009-11-21 21:52:02 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
/** {@inheritdoc} */
|
2009-12-17 16:37:47 +03:00
|
|
|
protected function _getInsertColumnList()
|
|
|
|
{
|
|
|
|
$columns = parent::_getInsertColumnList();
|
2011-09-07 08:48:19 +04:00
|
|
|
|
2009-12-17 16:37:47 +03:00
|
|
|
// Add discriminator column to the INSERT SQL
|
2010-03-05 19:35:00 +03:00
|
|
|
$columns[] = $this->_class->discriminatorColumn['name'];
|
|
|
|
|
2009-12-17 16:37:47 +03:00
|
|
|
return $columns;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
/** {@inheritdoc} */
|
2011-03-10 01:14:54 +03:00
|
|
|
protected function _getSQLTableAlias($className, $assocName = '')
|
2010-03-05 19:35:00 +03:00
|
|
|
{
|
2011-03-10 01:14:54 +03:00
|
|
|
return parent::_getSQLTableAlias($this->_class->rootEntityName, $assocName);
|
2010-04-26 15:02:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
protected function _getSelectConditionSQL(array $criteria, $assoc = null)
|
|
|
|
{
|
|
|
|
$conditionSql = parent::_getSelectConditionSQL($criteria, $assoc);
|
|
|
|
|
|
|
|
// Append discriminator condition
|
|
|
|
if ($conditionSql) $conditionSql .= ' AND ';
|
2011-09-07 08:48:19 +04:00
|
|
|
|
2010-07-04 15:34:14 +04:00
|
|
|
$values = array();
|
2011-09-07 08:48:19 +04:00
|
|
|
|
2010-07-04 15:34:14 +04:00
|
|
|
if ($this->_class->discriminatorValue !== null) { // discriminators can be 0
|
|
|
|
$values[] = $this->_conn->quote($this->_class->discriminatorValue);
|
|
|
|
}
|
|
|
|
|
2011-11-23 17:40:47 +04:00
|
|
|
$discrValues = array_flip($this->_class->discriminatorMap);
|
2011-09-07 08:48:19 +04:00
|
|
|
|
2011-11-23 17:40:47 +04:00
|
|
|
foreach ($this->_class->subClasses as $subclassName) {
|
|
|
|
$values[] = $this->_conn->quote($discrValues[$subclassName]);
|
2010-03-05 19:35:00 +03:00
|
|
|
}
|
2011-09-07 08:48:19 +04:00
|
|
|
|
|
|
|
$conditionSql .= $this->_getSQLTableAlias($this->_class->name) . '.' . $this->_class->discriminatorColumn['name']
|
|
|
|
. ' IN (' . implode(', ', $values) . ')';
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-04-26 15:02:30 +04:00
|
|
|
return $conditionSql;
|
2009-11-21 21:52:02 +03:00
|
|
|
}
|
2011-12-05 21:56:44 +04:00
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
protected function generateFilterConditionSQL(ClassMetadata $targetEntity, $targetTableAlias)
|
|
|
|
{
|
|
|
|
// Ensure that the filters are applied to the root entity of the inheritance tree
|
|
|
|
$realTargetEntity = $targetEntity;
|
|
|
|
if($targetEntity->name !== $targetEntity->rootEntityName) {
|
|
|
|
$realTargetEntity = $this->_em->getClassMetadata($targetEntity->rootEntityName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::generateFilterConditionSQL($realTargetEntity, $targetTableAlias);
|
|
|
|
}
|
2011-11-23 03:15:23 +04:00
|
|
|
}
|