2009-05-28 15:13:12 +04:00
|
|
|
<?php
|
2008-05-18 00:04:56 +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-05-18 00:04:56 +04:00
|
|
|
*/
|
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
namespace Doctrine\ORM\Persisters;
|
|
|
|
|
2012-06-19 02:01:03 +04:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
|
|
|
use Doctrine\ORM\Query\ResultSetMapping;
|
|
|
|
|
|
|
|
use Doctrine\DBAL\LockMode;
|
|
|
|
use Doctrine\DBAL\Types\Type;
|
|
|
|
|
|
|
|
use Doctrine\Common\Collections\Criteria;
|
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>
|
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
|
|
|
|
* @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.
|
2010-10-30 21:33:20 +04:00
|
|
|
*
|
|
|
|
* @var array
|
2009-05-28 15:33:50 +04:00
|
|
|
*/
|
|
|
|
private $_owningTableMap = array();
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2010-10-30 21:33:20 +04:00
|
|
|
/**
|
|
|
|
* Map of table to quoted table names.
|
2011-10-28 02:50:10 +04:00
|
|
|
*
|
2010-10-30 21:33:20 +04:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_quotedTableMap = array();
|
|
|
|
|
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
|
|
|
{
|
2011-09-07 08:48:19 +04:00
|
|
|
$class = ($this->_class->name !== $this->_class->rootEntityName)
|
|
|
|
? $this->_em->getClassMetadata($this->_class->rootEntityName)
|
|
|
|
: $this->_class;
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2011-09-07 08:48:19 +04:00
|
|
|
return $class->getTableName();
|
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.
|
|
|
|
*
|
2011-12-12 00:52:29 +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'];
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
return $this->_em->getClassMetadata($definingClassName);
|
2009-07-18 01:55:56 +04:00
|
|
|
}
|
2011-10-28 02:50:10 +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)
|
|
|
|
{
|
2011-09-07 08:48:19 +04:00
|
|
|
if (isset($this->_owningTableMap[$fieldName])) {
|
|
|
|
return $this->_owningTableMap[$fieldName];
|
|
|
|
}
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2011-09-07 08:48:19 +04:00
|
|
|
if (isset($this->_class->associationMappings[$fieldName]['inherited'])) {
|
|
|
|
$cm = $this->_em->getClassMetadata($this->_class->associationMappings[$fieldName]['inherited']);
|
|
|
|
} else if (isset($this->_class->fieldMappings[$fieldName]['inherited'])) {
|
|
|
|
$cm = $this->_em->getClassMetadata($this->_class->fieldMappings[$fieldName]['inherited']);
|
|
|
|
} else {
|
|
|
|
$cm = $this->_class;
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-09-07 08:48:19 +04:00
|
|
|
$tableName = $cm->getTableName();
|
|
|
|
|
|
|
|
$this->_owningTableMap[$fieldName] = $tableName;
|
2012-06-12 03:14:17 +04:00
|
|
|
$this->_quotedTableMap[$tableName] = $this->quoteStrategy->getTableName($cm, $this->_platform);
|
2011-09-07 08:48:19 +04:00
|
|
|
|
|
|
|
return $tableName;
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
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
|
|
|
|
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
|
2011-09-07 08:48:19 +04:00
|
|
|
$rootClass = ($this->_class->name !== $this->_class->rootEntityName) ? $this->_em->getClassMetadata($this->_class->rootEntityName) : $this->_class;
|
2009-12-18 15:30:19 +03:00
|
|
|
$rootPersister = $this->_em->getUnitOfWork()->getEntityPersister($rootClass->name);
|
2011-09-07 08:48:19 +04:00
|
|
|
$rootTableName = $rootClass->getTableName();
|
2010-04-26 15:02:30 +04: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();
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2009-12-18 15:30:19 +03:00
|
|
|
if ($rootClass !== $this->_class) {
|
2011-09-07 08:48:19 +04:00
|
|
|
$subTableStmts[$this->_class->getTableName()] = $this->_conn->prepare($this->_getInsertSQL());
|
2009-12-17 16:37:47 +03:00
|
|
|
}
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2009-12-17 16:37:47 +03:00
|
|
|
foreach ($this->_class->parentClasses as $parentClassName) {
|
|
|
|
$parentClass = $this->_em->getClassMetadata($parentClassName);
|
2011-09-07 08:48:19 +04:00
|
|
|
$parentTableName = $parentClass->getTableName();
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2009-12-18 15:30:19 +03:00
|
|
|
if ($parentClass !== $rootClass) {
|
|
|
|
$parentPersister = $this->_em->getUnitOfWork()->getEntityPersister($parentClassName);
|
2010-04-26 15:02:30 +04: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;
|
2011-10-28 02:50:10 +04:00
|
|
|
|
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
|
|
|
}
|
2011-10-28 02:50:10 +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;
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2011-09-05 09:27:06 +04:00
|
|
|
foreach ((array) $id as $idName => $idVal) {
|
|
|
|
$type = isset($this->_columnTypes[$idName]) ? $this->_columnTypes[$idName] : Type::STRING;
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2011-09-05 09:27:06 +04:00
|
|
|
$stmt->bindValue($paramIndex++, $idVal, $type);
|
2010-03-29 17:20:41 +04:00
|
|
|
}
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
foreach ($data as $columnName => $value) {
|
|
|
|
$stmt->bindValue($paramIndex++, $value, $this->_columnTypes[$columnName]);
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
2011-10-28 02:50:10 +04:00
|
|
|
|
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();
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2009-12-18 15:30:19 +03:00
|
|
|
foreach ($subTableStmts as $stmt) {
|
2009-08-11 14:51:38 +04:00
|
|
|
$stmt->closeCursor();
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2011-01-23 14:48:28 +03:00
|
|
|
if ($this->_class->isVersioned) {
|
|
|
|
$this->assignDefaultVersionValue($entity, $id);
|
2009-07-17 22:13:03 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2011-09-05 09:27:06 +04:00
|
|
|
if (($isVersioned = $this->_class->isVersioned) != false) {
|
2010-10-30 21:54:36 +04:00
|
|
|
$versionedClass = $this->_getVersionedClassMetadata();
|
2011-09-07 08:48:19 +04:00
|
|
|
$versionedTable = $versionedClass->getTableName();
|
2009-07-18 01:55:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($updateData) {
|
|
|
|
foreach ($updateData as $tableName => $data) {
|
2011-09-07 08:48:19 +04:00
|
|
|
$this->_updateTable(
|
|
|
|
$entity, $this->_quotedTableMap[$tableName], $data, $isVersioned && $versionedTable == $tableName
|
|
|
|
);
|
2009-07-18 01:55:56 +04:00
|
|
|
}
|
2011-10-28 02:50:10 +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])) {
|
2012-06-12 03:14:17 +04:00
|
|
|
$this->_updateTable($entity, $this->quoteStrategy->getTableName($versionedClass, $this->_platform), array(), true);
|
2010-11-10 01:15:14 +03:00
|
|
|
|
|
|
|
$id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity);
|
2011-01-23 14:48:28 +03:00
|
|
|
$this->assignDefaultVersionValue($entity, $id);
|
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)
|
|
|
|
{
|
2010-07-10 00:55:30 +04:00
|
|
|
$identifier = $this->_em->getUnitOfWork()->getEntityIdentifier($entity);
|
|
|
|
$this->deleteJoinTableRecords($identifier);
|
|
|
|
|
|
|
|
$id = array_combine($this->_class->getIdentifierColumnNames(), $identifier);
|
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()) {
|
2011-09-07 08:48:19 +04:00
|
|
|
$this->_conn->delete(
|
2012-06-12 03:14:17 +04:00
|
|
|
$this->quoteStrategy->getTableName($this->_em->getClassMetadata($this->_class->rootEntityName), $this->_platform), $id
|
2011-09-07 08:48:19 +04:00
|
|
|
);
|
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.
|
2012-06-12 03:14:17 +04:00
|
|
|
$this->_conn->delete($this->quoteStrategy->getTableName($this->_class, $this->_platform), $id);
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
foreach ($this->_class->parentClasses as $parentClass) {
|
2011-09-07 08:48:19 +04:00
|
|
|
$this->_conn->delete(
|
2012-06-12 03:14:17 +04:00
|
|
|
$this->quoteStrategy->getTableName($this->_em->getClassMetadata($parentClass), $this->_platform), $id
|
2011-09-07 08:48:19 +04:00
|
|
|
);
|
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
|
|
|
*/
|
2012-06-19 02:01:03 +04:00
|
|
|
protected function _getSelectEntitiesSQL($criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null)
|
2009-05-28 15:33:50 +04:00
|
|
|
{
|
|
|
|
$idColumns = $this->_class->getIdentifierColumnNames();
|
2010-04-26 15:02:30 +04:00
|
|
|
$baseTableAlias = $this->_getSQLTableAlias($this->_class->name);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-04-26 15:02:30 +04:00
|
|
|
// Create the column list fragment only once
|
2010-03-05 19:35:00 +03:00
|
|
|
if ($this->_selectColumnListSql === null) {
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2011-03-06 23:26:54 +03:00
|
|
|
$this->_rsm = new ResultSetMapping();
|
|
|
|
$this->_rsm->addEntityResult($this->_class->name, 'r');
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
// Add regular columns
|
|
|
|
$columnList = '';
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
foreach ($this->_class->fieldMappings as $fieldName => $mapping) {
|
|
|
|
if ($columnList != '') $columnList .= ', ';
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2011-09-07 08:48:19 +04:00
|
|
|
$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
|
2010-04-26 15:02:30 +04:00
|
|
|
foreach ($this->_class->associationMappings as $assoc2) {
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($assoc2['isOwningSide'] && $assoc2['type'] & ClassMetadata::TO_ONE) {
|
2011-09-07 08:48:19 +04:00
|
|
|
$tableAlias = isset($assoc2['inherited']) ? $this->_getSQLTableAlias($assoc2['inherited']) : $baseTableAlias;
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
foreach ($assoc2['targetToSourceKeyColumns'] as $srcColumn) {
|
2010-12-28 16:56:13 +03:00
|
|
|
if ($columnList != '') $columnList .= ', ';
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2011-09-07 08:48:19 +04:00
|
|
|
$columnList .= $this->getSelectJoinColumnSQL(
|
2011-10-28 02:50:10 +04:00
|
|
|
$tableAlias,
|
2011-09-07 08:48:19 +04:00
|
|
|
$srcColumn,
|
2010-12-28 16:56:13 +03:00
|
|
|
isset($assoc2['inherited']) ? $assoc2['inherited'] : $this->_class->name
|
|
|
|
);
|
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-04-26 15:02:30 +04:00
|
|
|
// Add discriminator column (DO NOT ALIAS, see AbstractEntityInheritancePersister#_processSQLResult).
|
2010-03-05 19:35:00 +03:00
|
|
|
$discrColumn = $this->_class->discriminatorColumn['name'];
|
2011-09-07 08:48:19 +04:00
|
|
|
$tableAlias = ($this->_class->rootEntityName == $this->_class->name) ? $baseTableAlias : $this->_getSQLTableAlias($this->_class->rootEntityName);
|
|
|
|
$columnList .= ', ' . $tableAlias . '.' . $discrColumn;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
|
|
|
$resultColumnName = $this->_platform->getSQLResultCasing($discrColumn);
|
2011-10-28 02:50:10 +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);
|
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 = '';
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
foreach ($this->_class->parentClasses as $parentClassName) {
|
|
|
|
$parentClass = $this->_em->getClassMetadata($parentClassName);
|
2010-04-26 15:02:30 +04:00
|
|
|
$tableAlias = $this->_getSQLTableAlias($parentClassName);
|
2012-06-12 03:14:17 +04:00
|
|
|
$joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->_platform) . ' ' . $tableAlias . ' ON ';
|
2009-05-28 15:33:50 +04:00
|
|
|
$first = true;
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
foreach ($idColumns as $idColumn) {
|
2009-10-22 23:12:00 +04:00
|
|
|
if ($first) $first = false; else $joinSql .= ' AND ';
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2009-10-22 23:12:00 +04:00
|
|
|
$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-04-26 15:02:30 +04:00
|
|
|
$tableAlias = $this->_getSQLTableAlias($subClassName);
|
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) {
|
2011-09-07 08:48:19 +04:00
|
|
|
if (isset($mapping['inherited'])) continue;
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
$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) {
|
2011-09-07 08:48:19 +04:00
|
|
|
if ($assoc2['isOwningSide'] && $assoc2['type'] & ClassMetadata::TO_ONE && ! isset($assoc2['inherited'])) {
|
2010-08-09 15:13:21 +04:00
|
|
|
foreach ($assoc2['targetToSourceKeyColumns'] as $srcColumn) {
|
2010-12-28 16:56:13 +03:00
|
|
|
if ($columnList != '') $columnList .= ', ';
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2011-09-07 08:48:19 +04:00
|
|
|
$columnList .= $this->getSelectJoinColumnSQL(
|
2011-10-28 02:50:10 +04:00
|
|
|
$tableAlias,
|
2011-09-07 08:48:19 +04:00
|
|
|
$srcColumn,
|
2010-12-28 16:56:13 +03:00
|
|
|
isset($assoc2['inherited']) ? $assoc2['inherited'] : $subClass->name
|
|
|
|
);
|
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
|
2012-06-12 03:14:17 +04:00
|
|
|
$joinSql .= ' LEFT JOIN ' . $this->quoteStrategy->getTableName($subClass, $this->_platform) . ' ' . $tableAlias . ' ON ';
|
2009-05-28 15:33:50 +04:00
|
|
|
$first = true;
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
foreach ($idColumns as $idColumn) {
|
2009-10-22 23:12:00 +04:00
|
|
|
if ($first) $first = false; else $joinSql .= ' AND ';
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2009-10-22 23:12:00 +04:00
|
|
|
$joinSql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2011-09-07 08:48:19 +04:00
|
|
|
$joinSql .= ($assoc != null && $assoc['type'] == ClassMetadata::MANY_TO_MANY) ? $this->_getSelectManyToManyJoinSQL($assoc) : '';
|
2010-04-26 15:02:30 +04:00
|
|
|
|
2012-06-19 02:01:03 +04:00
|
|
|
$conditionSql = ($criteria instanceof Criteria)
|
|
|
|
? $this->_getSelectConditionCriteriaSQL($criteria)
|
|
|
|
: $this->_getSelectConditionSQL($criteria, $assoc);
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2011-12-05 21:26:56 +04:00
|
|
|
// If the current class in the root entity, add the filters
|
2011-12-11 22:29:36 +04:00
|
|
|
if ($filterSql = $this->generateFilterConditionSQL($this->_em->getClassMetadata($this->_class->rootEntityName), $this->_getSQLTableAlias($this->_class->rootEntityName))) {
|
|
|
|
if ($conditionSql) {
|
|
|
|
$conditionSql .= ' AND ';
|
2011-12-05 21:26:56 +04:00
|
|
|
}
|
2011-12-11 22:29:36 +04:00
|
|
|
|
|
|
|
$conditionSql .= $filterSql;
|
2011-12-05 21:26:56 +04:00
|
|
|
}
|
|
|
|
|
2011-05-01 01:18:24 +04:00
|
|
|
$orderBy = ($assoc !== null && isset($assoc['orderBy'])) ? $assoc['orderBy'] : $orderBy;
|
|
|
|
$orderBySql = $orderBy ? $this->_getOrderBySQL($orderBy, $baseTableAlias) : '';
|
2010-03-05 19:35:00 +03:00
|
|
|
|
|
|
|
if ($this->_selectColumnListSql === null) {
|
|
|
|
$this->_selectColumnListSql = $columnList;
|
2010-02-16 01:50:35 +03:00
|
|
|
}
|
|
|
|
|
2010-12-04 21:44:10 +03:00
|
|
|
$lockSql = '';
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2010-12-04 21:44:10 +03:00
|
|
|
if ($lockMode == LockMode::PESSIMISTIC_READ) {
|
|
|
|
$lockSql = ' ' . $this->_platform->getReadLockSql();
|
|
|
|
} else if ($lockMode == LockMode::PESSIMISTIC_WRITE) {
|
|
|
|
$lockSql = ' ' . $this->_platform->getWriteLockSql();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_platform->modifyLimitQuery('SELECT ' . $this->_selectColumnListSql
|
2012-06-12 03:14:17 +04:00
|
|
|
. ' FROM ' . $this->quoteStrategy->getTableName($this->_class, $this->_platform) . ' ' . $baseTableAlias
|
2009-10-22 23:12:00 +04:00
|
|
|
. $joinSql
|
2010-12-04 21:44:10 +03:00
|
|
|
. ($conditionSql != '' ? ' WHERE ' . $conditionSql : '') . $orderBySql, $limit, $offset)
|
|
|
|
. $lockSql;
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
2010-05-02 15:02:44 +04:00
|
|
|
|
|
|
|
/**
|
2010-07-04 16:37:17 +04:00
|
|
|
* Get the FROM and optionally JOIN conditions to lock the entity managed by this persister.
|
2010-05-02 15:02:44 +04:00
|
|
|
*
|
2010-07-04 16:37:17 +04:00
|
|
|
* @return string
|
2010-05-02 15:02:44 +04:00
|
|
|
*/
|
2010-07-04 16:37:17 +04:00
|
|
|
public function getLockTablesSql()
|
2010-05-02 15:02:44 +04:00
|
|
|
{
|
2010-12-15 01:26:40 +03:00
|
|
|
$idColumns = $this->_class->getIdentifierColumnNames();
|
2010-07-04 16:37:17 +04:00
|
|
|
$baseTableAlias = $this->_getSQLTableAlias($this->_class->name);
|
|
|
|
|
|
|
|
// INNER JOIN parent tables
|
|
|
|
$joinSql = '';
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2010-07-04 16:37:17 +04:00
|
|
|
foreach ($this->_class->parentClasses as $parentClassName) {
|
|
|
|
$parentClass = $this->_em->getClassMetadata($parentClassName);
|
|
|
|
$tableAlias = $this->_getSQLTableAlias($parentClassName);
|
2012-06-12 03:14:17 +04:00
|
|
|
$joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->_platform) . ' ' . $tableAlias . ' ON ';
|
2010-07-04 16:37:17 +04:00
|
|
|
$first = true;
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2010-07-04 16:37:17 +04:00
|
|
|
foreach ($idColumns as $idColumn) {
|
|
|
|
if ($first) $first = false; else $joinSql .= ' AND ';
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2010-07-04 16:37:17 +04:00
|
|
|
$joinSql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-12 03:14:17 +04:00
|
|
|
return 'FROM ' .$this->quoteStrategy->getTableName($this->_class, $this->_platform) . ' ' . $baseTableAlias . $joinSql;
|
2010-05-02 15:02:44 +04:00
|
|
|
}
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2010-04-26 15:02:30 +04:00
|
|
|
/* Ensure this method is never called. This persister overrides _getSelectEntitiesSQL directly. */
|
2010-03-05 19:35:00 +03:00
|
|
|
protected function _getSelectColumnListSQL()
|
|
|
|
{
|
2010-04-26 15:02:30 +04:00
|
|
|
throw new \BadMethodCallException("Illegal invocation of ".__METHOD__.".");
|
2010-03-05 19:35:00 +03:00
|
|
|
}
|
2011-10-28 02:50:10 +04:00
|
|
|
|
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'])
|
2010-08-09 15:13:21 +04:00
|
|
|
|| isset($this->_class->associationMappings[$name]['inherited'])
|
2009-12-17 16:37:47 +03:00
|
|
|
|| ($this->_class->isVersioned && $this->_class->versionField == $name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->_class->associationMappings[$name])) {
|
|
|
|
$assoc = $this->_class->associationMappings[$name];
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($assoc['type'] & ClassMetadata::TO_ONE && $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) {
|
2012-09-30 22:40:19 +04:00
|
|
|
$columns[] = $this->quoteStrategy->getColumnName($name, $this->_class, $this->_platform);
|
|
|
|
$this->_columnTypes[$name] = $this->_class->fieldMappings[$name]['type'];
|
2009-12-17 16:37:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2011-01-23 14:48:28 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function assignDefaultVersionValue($entity, $id)
|
|
|
|
{
|
|
|
|
$value = $this->fetchVersionValue($this->_getVersionedClassMetadata(), $id);
|
|
|
|
$this->_class->setFieldValue($entity, $this->_class->versionField, $value);
|
|
|
|
}
|
2011-12-01 02:01:10 +04:00
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|