2008-09-12 13:44:51 +04:00
|
|
|
<?php
|
2009-05-03 15:49:48 +04: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>.
|
|
|
|
*/
|
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.
|
|
|
|
*
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @version $Revision: 3406 $
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
2009-05-18 00:45:50 +04:00
|
|
|
class SingleTablePersister extends StandardEntityPersister
|
2009-02-20 08:46:20 +03:00
|
|
|
{
|
2009-04-09 22:12:48 +04:00
|
|
|
/** @override */
|
|
|
|
protected function _prepareData($entity, array &$result, $isInsert = false)
|
|
|
|
{
|
|
|
|
parent::_prepareData($entity, $result, $isInsert);
|
|
|
|
// Populate the discriminator column
|
|
|
|
if ($isInsert) {
|
2010-03-05 19:35:00 +03:00
|
|
|
$discColumn = $this->_class->discriminatorColumn['name'];
|
2009-08-16 20:36:00 +04:00
|
|
|
$result[$this->_class->getQuotedTableName($this->_platform)][$discColumn] =
|
2009-05-21 12:53:40 +04:00
|
|
|
$this->_class->discriminatorValue;
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
|
|
|
}
|
2009-11-21 21:52:02 +03:00
|
|
|
|
|
|
|
/** @override */
|
2010-03-05 19:35:00 +03:00
|
|
|
protected function _getSelectColumnListSQL()
|
2009-11-21 21:52:02 +03:00
|
|
|
{
|
2010-03-05 19:35:00 +03:00
|
|
|
$columnList = parent::_getSelectColumnListSQL();
|
2009-11-21 21:52:02 +03:00
|
|
|
// Append discriminator column
|
2010-03-05 19:35:00 +03:00
|
|
|
$discrColumn = $this->_class->discriminatorColumn['name'];
|
|
|
|
$columnList .= ", $discrColumn";
|
|
|
|
$rootClass = $this->_em->getClassMetadata($this->_class->rootEntityName);
|
|
|
|
$tableAlias = $this->_getSQLTableAlias($rootClass);
|
|
|
|
$resultColumnName = $this->_platform->getSQLResultCasing($discrColumn);
|
|
|
|
$this->_resultColumnNames[$resultColumnName] = $discrColumn;
|
|
|
|
|
2009-11-21 21:52:02 +03:00
|
|
|
foreach ($this->_class->subClasses as $subClassName) {
|
|
|
|
$subClass = $this->_em->getClassMetadata($subClassName);
|
|
|
|
// Append subclass columns
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-11-21 21:52:02 +03:00
|
|
|
// Append subclass foreign keys
|
|
|
|
foreach ($subClass->associationMappings as $assoc) {
|
|
|
|
if ($assoc->isOwningSide && $assoc->isOneToOne() && ! isset($subClass->inheritedAssociationFields[$assoc->sourceFieldName])) {
|
|
|
|
foreach ($assoc->targetToSourceKeyColumns as $srcColumn) {
|
2010-03-05 19:35:00 +03:00
|
|
|
$columnAlias = $srcColumn . $this->_sqlAliasCounter++;
|
|
|
|
$columnList .= ', ' . $tableAlias . ".$srcColumn AS $columnAlias";
|
|
|
|
$resultColumnName = $this->_platform->getSQLResultCasing($columnAlias);
|
|
|
|
if ( ! isset($this->_resultColumnNames[$resultColumnName])) {
|
2009-12-09 15:37:57 +03:00
|
|
|
$this->_resultColumnNames[$resultColumnName] = $srcColumn;
|
|
|
|
}
|
2009-11-21 21:52:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $columnList;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-12-17 16:37:47 +03:00
|
|
|
/** @override */
|
|
|
|
protected function _getInsertColumnList()
|
|
|
|
{
|
|
|
|
$columns = parent::_getInsertColumnList();
|
|
|
|
// 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
|
|
|
|
2009-11-21 21:52:02 +03:00
|
|
|
/** @override */
|
2010-03-05 19:35:00 +03:00
|
|
|
protected function _processSQLResult(array $sqlResult)
|
2009-11-21 21:52:02 +03:00
|
|
|
{
|
2010-03-05 19:35:00 +03:00
|
|
|
return $this->_processSQLResultInheritanceAware($sqlResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @override */
|
|
|
|
protected function _getSQLTableAlias(ClassMetadata $class)
|
|
|
|
{
|
|
|
|
if (isset($this->_sqlTableAliases[$class->rootEntityName])) {
|
|
|
|
return $this->_sqlTableAliases[$class->rootEntityName];
|
|
|
|
}
|
|
|
|
$tableAlias = $this->_em->getClassMetadata($class->rootEntityName)->primaryTable['name'][0] . $this->_sqlAliasCounter++;
|
|
|
|
$this->_sqlTableAliases[$class->rootEntityName] = $tableAlias;
|
|
|
|
|
|
|
|
return $tableAlias;
|
2009-11-21 21:52:02 +03:00
|
|
|
}
|
2009-02-20 08:46:20 +03:00
|
|
|
}
|