2009-05-28 15:13:12 +04:00
|
|
|
<?php
|
2008-05-18 00:04:56 +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
|
2009-05-03 15:49:48 +04:00
|
|
|
* <http://www.doctrine-project.org>.
|
2008-05-18 00:04:56 +04:00
|
|
|
*/
|
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
namespace Doctrine\ORM\Persisters;
|
|
|
|
|
2009-12-09 15:37:57 +03:00
|
|
|
use Doctrine\ORM\ORMException;
|
2009-05-14 14:03:09 +04:00
|
|
|
|
2008-05-18 00:04:56 +04:00
|
|
|
/**
|
2008-07-10 21:17:58 +04:00
|
|
|
* The joined subclass persister maps a single entity instance to several tables in the
|
2010-03-29 17:20:41 +04:00
|
|
|
* database as it is defined by the <tt>Class Table Inheritance</tt> strategy.
|
2008-05-18 00:04:56 +04:00
|
|
|
*
|
2010-03-29 17:20:41 +04:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @see http://martinfowler.com/eaaCatalog/classTableInheritance.html
|
2008-05-18 00:04:56 +04:00
|
|
|
*/
|
2010-03-29 17:20:41 +04:00
|
|
|
class JoinedSubclassPersister extends AbstractEntityInheritancePersister
|
2009-05-21 12:53:40 +04:00
|
|
|
{
|
2010-03-29 17:20:41 +04:00
|
|
|
/**
|
|
|
|
* Map that maps column names to the table names that own them.
|
|
|
|
* This is mainly a temporary cache, used during a single request.
|
2009-05-28 15:33:50 +04:00
|
|
|
*/
|
|
|
|
private $_owningTableMap = array();
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2010-03-29 17:20:41 +04:00
|
|
|
protected function _getDiscriminatorColumnTableName()
|
2009-05-28 15:33:50 +04:00
|
|
|
{
|
2010-03-29 17:20:41 +04:00
|
|
|
if ($this->_class->name == $this->_class->rootEntityName) {
|
|
|
|
return $this->_class->table['name'];
|
|
|
|
} else {
|
|
|
|
return $this->_em->getClassMetadata($this->_class->rootEntityName)->table['name'];
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-07-18 01:55:56 +04:00
|
|
|
/**
|
2010-03-29 17:20:41 +04:00
|
|
|
* This function finds the ClassMetadata instance in an inheritance hierarchy
|
2009-07-18 01:55:56 +04:00
|
|
|
* that is responsible for enabling versioning.
|
|
|
|
*
|
2010-03-29 17:20:41 +04:00
|
|
|
* @return Doctrine\ORM\Mapping\ClassMetadata
|
2009-07-18 01:55:56 +04:00
|
|
|
*/
|
|
|
|
private function _getVersionedClassMetadata()
|
|
|
|
{
|
2010-03-29 17:20:41 +04:00
|
|
|
if (isset($this->_class->fieldMappings[$this->_class->versionField]['inherited'])) {
|
|
|
|
$definingClassName = $this->_class->fieldMappings[$this->_class->versionField]['inherited'];
|
|
|
|
return $this->_em->getClassMetadata($definingClassName);
|
2009-07-18 01:55:56 +04:00
|
|
|
}
|
2010-03-29 17:20:41 +04:00
|
|
|
return $this->_class;
|
2009-07-18 01:55:56 +04:00
|
|
|
}
|
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
/**
|
2009-11-21 21:52:02 +03:00
|
|
|
* Gets the name of the table that owns the column the given field is mapped to.
|
2009-05-28 15:33:50 +04:00
|
|
|
*
|
2009-11-21 21:52:02 +03:00
|
|
|
* @param string $fieldName
|
|
|
|
* @return string
|
2009-05-28 15:33:50 +04:00
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getOwningTable($fieldName)
|
|
|
|
{
|
|
|
|
if ( ! isset($this->_owningTableMap[$fieldName])) {
|
|
|
|
if (isset($this->_class->associationMappings[$fieldName])) {
|
|
|
|
if (isset($this->_class->inheritedAssociationFields[$fieldName])) {
|
|
|
|
$this->_owningTableMap[$fieldName] = $this->_em->getClassMetadata(
|
2009-12-17 16:37:47 +03:00
|
|
|
$this->_class->inheritedAssociationFields[$fieldName]
|
2010-03-29 17:20:41 +04:00
|
|
|
)->table['name'];
|
2009-05-28 15:33:50 +04:00
|
|
|
} else {
|
2010-03-29 17:20:41 +04:00
|
|
|
$this->_owningTableMap[$fieldName] = $this->_class->table['name'];
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
|
|
|
} else if (isset($this->_class->fieldMappings[$fieldName]['inherited'])) {
|
|
|
|
$this->_owningTableMap[$fieldName] = $this->_em->getClassMetadata(
|
2009-12-17 16:37:47 +03:00
|
|
|
$this->_class->fieldMappings[$fieldName]['inherited']
|
2010-03-29 17:20:41 +04:00
|
|
|
)->table['name'];
|
2009-05-28 15:33:50 +04:00
|
|
|
} else {
|
2010-03-29 17:20:41 +04:00
|
|
|
$this->_owningTableMap[$fieldName] = $this->_class->table['name'];
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
return $this->_owningTableMap[$fieldName];
|
|
|
|
}
|
2008-05-18 00:04:56 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function executeInserts()
|
|
|
|
{
|
|
|
|
if ( ! $this->_queuedInserts) {
|
|
|
|
return;
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
if ($this->_class->isVersioned) {
|
2009-07-18 01:55:56 +04:00
|
|
|
$versionedClass = $this->_getVersionedClassMetadata();
|
2009-07-17 22:13:03 +04:00
|
|
|
}
|
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
$postInsertIds = array();
|
|
|
|
$idGen = $this->_class->idGenerator;
|
|
|
|
$isPostInsertId = $idGen->isPostInsertGenerator();
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-12-18 15:30:19 +03:00
|
|
|
// Prepare statement for the root table
|
|
|
|
$rootClass = $this->_class->name == $this->_class->rootEntityName ?
|
|
|
|
$this->_class : $this->_em->getClassMetadata($this->_class->rootEntityName);
|
|
|
|
$rootPersister = $this->_em->getUnitOfWork()->getEntityPersister($rootClass->name);
|
2010-03-29 17:20:41 +04:00
|
|
|
$rootTableName = $rootClass->table['name'];
|
2010-03-05 19:35:00 +03:00
|
|
|
$rootTableStmt = $this->_conn->prepare($rootPersister->getInsertSQL());
|
2010-03-29 17:20:41 +04:00
|
|
|
|
2009-12-18 15:30:19 +03:00
|
|
|
// Prepare statements for sub tables.
|
|
|
|
$subTableStmts = array();
|
|
|
|
if ($rootClass !== $this->_class) {
|
2010-03-29 17:20:41 +04:00
|
|
|
$subTableStmts[$this->_class->table['name']] = $this->_conn->prepare($this->getInsertSQL());
|
2009-12-17 16:37:47 +03:00
|
|
|
}
|
|
|
|
foreach ($this->_class->parentClasses as $parentClassName) {
|
|
|
|
$parentClass = $this->_em->getClassMetadata($parentClassName);
|
2010-03-29 17:20:41 +04:00
|
|
|
$parentTableName = $parentClass->table['name'];
|
2009-12-18 15:30:19 +03:00
|
|
|
if ($parentClass !== $rootClass) {
|
|
|
|
$parentPersister = $this->_em->getUnitOfWork()->getEntityPersister($parentClassName);
|
2010-03-05 19:35:00 +03:00
|
|
|
$subTableStmts[$parentTableName] = $this->_conn->prepare($parentPersister->getInsertSQL());
|
2009-12-17 16:37:47 +03:00
|
|
|
}
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
2010-03-29 17:20:41 +04:00
|
|
|
|
2009-12-18 15:30:19 +03:00
|
|
|
// Execute all inserts. For each entity:
|
|
|
|
// 1) Insert on root table
|
|
|
|
// 2) Insert on sub tables
|
2009-05-28 15:33:50 +04:00
|
|
|
foreach ($this->_queuedInserts as $entity) {
|
2010-03-29 17:20:41 +04:00
|
|
|
$insertData = $this->_prepareInsertData($entity);
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
// Execute insert on root table
|
|
|
|
$paramIndex = 1;
|
2010-03-29 17:20:41 +04:00
|
|
|
foreach ($insertData[$rootTableName] as $columnName => $value) {
|
|
|
|
$rootTableStmt->bindValue($paramIndex++, $value, $this->_columnTypes[$columnName]);
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
2009-12-18 15:30:19 +03:00
|
|
|
$rootTableStmt->execute();
|
2008-05-18 00:04:56 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
if ($isPostInsertId) {
|
|
|
|
$id = $idGen->generate($this->_em, $entity);
|
|
|
|
$postInsertIds[$id] = $entity;
|
|
|
|
} else {
|
|
|
|
$id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity);
|
|
|
|
}
|
2008-05-18 00:04:56 +04:00
|
|
|
|
2009-12-18 15:30:19 +03:00
|
|
|
// Execute inserts on subtables.
|
|
|
|
// The order doesn't matter because all child tables link to the root table via FK.
|
|
|
|
foreach ($subTableStmts as $tableName => $stmt) {
|
|
|
|
$data = isset($insertData[$tableName]) ? $insertData[$tableName] : array();
|
2009-05-28 15:33:50 +04:00
|
|
|
$paramIndex = 1;
|
2010-03-29 17:20:41 +04:00
|
|
|
foreach ((array) $id as $idVal) {
|
|
|
|
$stmt->bindValue($paramIndex++, $idVal);
|
|
|
|
}
|
|
|
|
foreach ($data as $columnName => $value) {
|
|
|
|
$stmt->bindValue($paramIndex++, $value, $this->_columnTypes[$columnName]);
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
|
|
|
$stmt->execute();
|
|
|
|
}
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-12-18 15:30:19 +03:00
|
|
|
$rootTableStmt->closeCursor();
|
|
|
|
foreach ($subTableStmts as $stmt) {
|
2009-08-11 14:51:38 +04:00
|
|
|
$stmt->closeCursor();
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
if (isset($versionedClass)) {
|
2009-07-17 22:13:03 +04:00
|
|
|
$this->_assignDefaultVersionValue($versionedClass, $entity, $id);
|
|
|
|
}
|
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
$this->_queuedInserts = array();
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
return $postInsertIds;
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
/**
|
2010-03-29 17:20:41 +04:00
|
|
|
* {@inheritdoc}
|
2009-05-28 15:33:50 +04:00
|
|
|
*/
|
|
|
|
public function update($entity)
|
|
|
|
{
|
2010-03-29 17:20:41 +04:00
|
|
|
$updateData = $this->_prepareUpdateData($entity);
|
2008-05-18 00:04:56 +04:00
|
|
|
|
2009-07-18 01:55:56 +04:00
|
|
|
if ($isVersioned = $this->_class->isVersioned) {
|
2010-03-29 17:20:41 +04:00
|
|
|
$versionedTable = $this->_getVersionedClassMetadata()->table['name'];
|
2009-07-18 01:55:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($updateData) {
|
|
|
|
foreach ($updateData as $tableName => $data) {
|
2010-03-29 17:20:41 +04:00
|
|
|
$this->_updateTable($entity, $tableName, $data, $isVersioned && $versionedTable == $tableName);
|
2009-07-18 01:55:56 +04:00
|
|
|
}
|
2010-03-29 17:20:41 +04:00
|
|
|
// Make sure the table with the version column is updated even if no columns on that
|
|
|
|
// table were affected.
|
2009-07-18 01:55:56 +04:00
|
|
|
if ($isVersioned && ! isset($updateData[$versionedTable])) {
|
2010-03-29 17:20:41 +04:00
|
|
|
$this->_updateTable($entity, $versionedTable, array(), true);
|
2009-07-18 01:55:56 +04:00
|
|
|
}
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
|
|
|
}
|
2008-05-18 00:04:56 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
/**
|
2010-03-29 17:20:41 +04:00
|
|
|
* {@inheritdoc}
|
2009-05-28 15:33:50 +04:00
|
|
|
*/
|
|
|
|
public function delete($entity)
|
|
|
|
{
|
|
|
|
$id = array_combine(
|
2010-02-20 01:49:03 +03:00
|
|
|
$this->_class->getIdentifierColumnNames(),
|
2009-07-03 21:36:41 +04:00
|
|
|
$this->_em->getUnitOfWork()->getEntityIdentifier($entity)
|
2009-05-28 15:33:50 +04:00
|
|
|
);
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
// If the database platform supports FKs, just
|
|
|
|
// delete the row from the root table. Cascades do the rest.
|
2010-03-29 17:20:41 +04:00
|
|
|
if ($this->_platform->supportsForeignKeyConstraints()) {
|
2009-05-28 15:33:50 +04:00
|
|
|
$this->_conn->delete($this->_em->getClassMetadata($this->_class->rootEntityName)
|
2010-03-29 17:20:41 +04:00
|
|
|
->getQuotedTableName($this->_platform), $id);
|
2009-05-28 15:33:50 +04:00
|
|
|
} else {
|
2009-08-11 14:51:38 +04:00
|
|
|
// Delete from all tables individually, starting from this class' table up to the root table.
|
2010-03-29 17:20:41 +04:00
|
|
|
$this->_conn->delete($this->_class->getQuotedTableName($this->_platform), $id);
|
2009-05-28 15:33:50 +04:00
|
|
|
foreach ($this->_class->parentClasses as $parentClass) {
|
2010-03-29 17:20:41 +04:00
|
|
|
$this->_conn->delete($this->_em->getClassMetadata($parentClass)->getQuotedTableName($this->_platform), $id);
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
/**
|
2010-03-29 17:20:41 +04:00
|
|
|
* {@inheritdoc}
|
2009-05-28 15:33:50 +04:00
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
protected function _getSelectEntitiesSQL(array &$criteria, $assoc = null, $orderBy = null)
|
2009-05-28 15:33:50 +04:00
|
|
|
{
|
|
|
|
$idColumns = $this->_class->getIdentifierColumnNames();
|
2010-03-05 19:35:00 +03:00
|
|
|
$baseTableAlias = $this->_getSQLTableAlias($this->_class);
|
|
|
|
|
|
|
|
if ($this->_selectColumnListSql === null) {
|
|
|
|
// Add regular columns
|
|
|
|
$columnList = '';
|
|
|
|
foreach ($this->_class->fieldMappings as $fieldName => $mapping) {
|
|
|
|
if ($columnList != '') $columnList .= ', ';
|
|
|
|
$columnList .= $this->_getSelectColumnSQL($fieldName,
|
|
|
|
isset($mapping['inherited']) ? $this->_em->getClassMetadata($mapping['inherited']) : $this->_class);
|
2009-12-09 15:37:57 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
|
|
|
// Add foreign key columns
|
|
|
|
foreach ($this->_class->associationMappings as $assoc) {
|
|
|
|
if ($assoc->isOwningSide && $assoc->isOneToOne()) {
|
|
|
|
$tableAlias = isset($this->_class->inheritedAssociationFields[$assoc->sourceFieldName]) ?
|
|
|
|
$this->_getSQLTableAlias($this->_em->getClassMetadata($this->_class->inheritedAssociationFields[$assoc->sourceFieldName]))
|
|
|
|
: $baseTableAlias;
|
|
|
|
foreach ($assoc->targetToSourceKeyColumns as $srcColumn) {
|
|
|
|
$columnAlias = $srcColumn . $this->_sqlAliasCounter++;
|
|
|
|
$columnList .= ", $tableAlias.$srcColumn AS $columnAlias";
|
|
|
|
$resultColumnName = $this->_platform->getSQLResultCasing($columnAlias);
|
|
|
|
if ( ! isset($this->_resultColumnNames[$resultColumnName])) {
|
|
|
|
$this->_resultColumnNames[$resultColumnName] = $srcColumn;
|
|
|
|
}
|
2009-12-09 15:37:57 +03:00
|
|
|
}
|
2009-10-22 16:50:58 +04:00
|
|
|
}
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
// Add discriminator column (DO NOT ALIAS THIS COLUMN, see StandardEntityPersister#_processSQLResultInheritanceAware).
|
2010-03-05 19:35:00 +03:00
|
|
|
$discrColumn = $this->_class->discriminatorColumn['name'];
|
|
|
|
if ($this->_class->rootEntityName == $this->_class->name) {
|
|
|
|
$columnList .= ", $baseTableAlias.$discrColumn";
|
|
|
|
} else {
|
|
|
|
$columnList .= ', ' . $this->_getSQLTableAlias($this->_em->getClassMetadata($this->_class->rootEntityName))
|
|
|
|
. ".$discrColumn";
|
|
|
|
}
|
|
|
|
|
|
|
|
$resultColumnName = $this->_platform->getSQLResultCasing($discrColumn);
|
|
|
|
$this->_resultColumnNames[$resultColumnName] = $discrColumn;
|
2009-12-09 15:37:57 +03:00
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
// INNER JOIN parent tables
|
2009-10-22 23:12:00 +04:00
|
|
|
$joinSql = '';
|
2009-05-28 15:33:50 +04:00
|
|
|
foreach ($this->_class->parentClasses as $parentClassName) {
|
|
|
|
$parentClass = $this->_em->getClassMetadata($parentClassName);
|
2010-03-05 19:35:00 +03:00
|
|
|
$tableAlias = $this->_getSQLTableAlias($parentClass);
|
2009-10-22 23:12:00 +04:00
|
|
|
$joinSql .= ' INNER JOIN ' . $parentClass->getQuotedTableName($this->_platform) . ' ' . $tableAlias . ' ON ';
|
2009-05-28 15:33:50 +04:00
|
|
|
$first = true;
|
|
|
|
foreach ($idColumns as $idColumn) {
|
2009-10-22 23:12:00 +04:00
|
|
|
if ($first) $first = false; else $joinSql .= ' AND ';
|
|
|
|
$joinSql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
// OUTER JOIN sub tables
|
|
|
|
foreach ($this->_class->subClasses as $subClassName) {
|
|
|
|
$subClass = $this->_em->getClassMetadata($subClassName);
|
2010-03-05 19:35:00 +03:00
|
|
|
$tableAlias = $this->_getSQLTableAlias($subClass);
|
2009-10-22 23:12:00 +04:00
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
if ($this->_selectColumnListSql === null) {
|
|
|
|
// Add subclass columns
|
|
|
|
foreach ($subClass->fieldMappings as $fieldName => $mapping) {
|
|
|
|
if (isset($mapping['inherited'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$columnList .= ', ' . $this->_getSelectColumnSQL($fieldName, $subClass);
|
2009-12-09 15:37:57 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
|
|
|
// Add join columns (foreign keys)
|
|
|
|
foreach ($subClass->associationMappings as $assoc2) {
|
|
|
|
if ($assoc2->isOwningSide && $assoc2->isOneToOne() && ! isset($subClass->inheritedAssociationFields[$assoc2->sourceFieldName])) {
|
|
|
|
foreach ($assoc2->targetToSourceKeyColumns as $srcColumn) {
|
|
|
|
$columnAlias = $srcColumn . $this->_sqlAliasCounter++;
|
|
|
|
$columnList .= ', ' . $tableAlias . ".$srcColumn AS $columnAlias";
|
|
|
|
$resultColumnName = $this->_platform->getSQLResultCasing($columnAlias);
|
|
|
|
if ( ! isset($this->_resultColumnNames[$resultColumnName])) {
|
|
|
|
$this->_resultColumnNames[$resultColumnName] = $srcColumn;
|
|
|
|
}
|
2009-12-09 15:37:57 +03:00
|
|
|
}
|
2009-10-22 23:12:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-10-22 23:12:00 +04:00
|
|
|
// Add LEFT JOIN
|
|
|
|
$joinSql .= ' LEFT JOIN ' . $subClass->getQuotedTableName($this->_platform) . ' ' . $tableAlias . ' ON ';
|
2009-05-28 15:33:50 +04:00
|
|
|
$first = true;
|
|
|
|
foreach ($idColumns as $idColumn) {
|
2009-10-22 23:12:00 +04:00
|
|
|
if ($first) $first = false; else $joinSql .= ' AND ';
|
|
|
|
$joinSql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
$conditionSql = '';
|
|
|
|
foreach ($criteria as $field => $value) {
|
|
|
|
if ($conditionSql != '') $conditionSql .= ' AND ';
|
2010-03-18 14:40:43 +03:00
|
|
|
if (isset($this->_class->fieldMappings[$field]['inherited'])) {
|
|
|
|
$conditionSql .= $this->_getSQLTableAlias($this->_em->getClassMetadata($this->_class->fieldMappings[$field]['inherited'])) . '.';
|
|
|
|
} else {
|
|
|
|
$conditionSql .= $baseTableAlias . '.';
|
|
|
|
}
|
2009-09-15 16:24:38 +04:00
|
|
|
if (isset($this->_class->columnNames[$field])) {
|
|
|
|
$conditionSql .= $this->_class->getQuotedColumnName($field, $this->_platform);
|
|
|
|
} else if ($assoc !== null) {
|
2010-03-05 19:35:00 +03:00
|
|
|
$conditionSql .= $field;
|
2009-09-15 16:24:38 +04:00
|
|
|
} else {
|
2009-12-09 15:37:57 +03:00
|
|
|
throw ORMException::unrecognizedField($field);
|
2009-09-15 16:24:38 +04:00
|
|
|
}
|
|
|
|
$conditionSql .= ' = ?';
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2010-02-16 01:50:35 +03:00
|
|
|
$orderBySql = '';
|
|
|
|
if ($orderBy !== null) {
|
2010-03-05 19:35:00 +03:00
|
|
|
$orderBySql = $this->_getCollectionOrderBySQL($orderBy, $baseTableAlias);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->_selectColumnListSql === null) {
|
|
|
|
$this->_selectColumnListSql = $columnList;
|
2010-02-16 01:50:35 +03:00
|
|
|
}
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
return 'SELECT ' . $this->_selectColumnListSql
|
2009-10-22 23:12:00 +04:00
|
|
|
. ' FROM ' . $this->_class->getQuotedTableName($this->_platform) . ' ' . $baseTableAlias
|
|
|
|
. $joinSql
|
2010-02-16 01:50:35 +03:00
|
|
|
. ($conditionSql != '' ? ' WHERE ' . $conditionSql : '') . $orderBySql;
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
2009-11-21 21:52:02 +03:00
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
/** Ensure this method is never called. This persister overrides _getSelectEntitiesSQL directly. */
|
2010-03-05 19:35:00 +03:00
|
|
|
protected function _getSelectColumnListSQL()
|
|
|
|
{
|
|
|
|
throw new \BadMethodCallException("Illegal invocation of ".__METHOD__." on JoinedSubclassPersister.");
|
|
|
|
}
|
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
/** {@inheritdoc} */
|
2009-12-17 16:37:47 +03:00
|
|
|
protected function _getInsertColumnList()
|
|
|
|
{
|
|
|
|
// Identifier columns must always come first in the column list of subclasses.
|
|
|
|
$columns = $this->_class->parentClasses ? $this->_class->getIdentifierColumnNames() : array();
|
|
|
|
|
|
|
|
foreach ($this->_class->reflFields as $name => $field) {
|
|
|
|
if (isset($this->_class->fieldMappings[$name]['inherited']) && ! isset($this->_class->fieldMappings[$name]['id'])
|
|
|
|
|| isset($this->_class->inheritedAssociationFields[$name])
|
|
|
|
|| ($this->_class->isVersioned && $this->_class->versionField == $name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->_class->associationMappings[$name])) {
|
|
|
|
$assoc = $this->_class->associationMappings[$name];
|
|
|
|
if ($assoc->isOneToOne() && $assoc->isOwningSide) {
|
|
|
|
foreach ($assoc->targetToSourceKeyColumns as $sourceCol) {
|
2010-03-05 19:35:00 +03:00
|
|
|
$columns[] = $sourceCol;
|
2009-12-17 16:37:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ($this->_class->name != $this->_class->rootEntityName ||
|
|
|
|
! $this->_class->isIdGeneratorIdentity() || $this->_class->identifier[0] != $name) {
|
|
|
|
$columns[] = $this->_class->getQuotedColumnName($name, $this->_platform);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add discriminator column if it is the topmost class.
|
|
|
|
if ($this->_class->name == $this->_class->rootEntityName) {
|
2010-03-05 19:35:00 +03:00
|
|
|
$columns[] = $this->_class->discriminatorColumn['name'];
|
2009-12-17 16:37:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $columns;
|
|
|
|
}
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|