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
|
|
|
*/
|
2012-07-22 20:42:43 +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
|
|
|
|
*/
|
2012-07-22 20:42:43 +04:00
|
|
|
private $quotedTableMap = array();
|
2010-10-30 21:33:20 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-07-22 20:42:43 +04:00
|
|
|
protected function getDiscriminatorColumnTableName()
|
2009-05-28 15:33:50 +04:00
|
|
|
{
|
2012-07-22 20:42:43 +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
|
|
|
*/
|
2012-07-22 20:42:43 +04:00
|
|
|
private function getVersionedClassMetadata()
|
2009-07-18 01:55:56 +04:00
|
|
|
{
|
2012-07-22 20:42:43 +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
|
|
|
|
2012-07-22 20:42:43 +04:00
|
|
|
return $this->em->getClassMetadata($definingClassName);
|
2009-07-18 01:55:56 +04:00
|
|
|
}
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2012-07-22 20:42:43 +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
|
2012-12-03 14:15:43 +04:00
|
|
|
*
|
2009-11-21 21:52:02 +03:00
|
|
|
* @return string
|
2012-12-03 14:15:43 +04:00
|
|
|
*
|
2009-05-28 15:33:50 +04:00
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getOwningTable($fieldName)
|
|
|
|
{
|
2012-07-22 20:42:43 +04:00
|
|
|
if (isset($this->owningTableMap[$fieldName])) {
|
|
|
|
return $this->owningTableMap[$fieldName];
|
2011-09-07 08:48:19 +04:00
|
|
|
}
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
switch (true) {
|
|
|
|
case isset($this->class->associationMappings[$fieldName]['inherited']):
|
|
|
|
$cm = $this->em->getClassMetadata($this->class->associationMappings[$fieldName]['inherited']);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case isset($this->class->fieldMappings[$fieldName]['inherited']):
|
|
|
|
$cm = $this->em->getClassMetadata($this->class->fieldMappings[$fieldName]['inherited']);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$cm = $this->class;
|
|
|
|
break;
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
$tableName = $cm->getTableName();
|
|
|
|
$quotedTableName = $this->quoteStrategy->getTableName($cm, $this->platform);
|
2011-09-07 08:48:19 +04:00
|
|
|
|
2012-07-22 20:42:43 +04:00
|
|
|
$this->owningTableMap[$fieldName] = $tableName;
|
2012-07-26 05:18:25 +04:00
|
|
|
$this->quotedTableMap[$tableName] = $quotedTableName;
|
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()
|
|
|
|
{
|
2012-07-22 20:42:43 +04:00
|
|
|
if ( ! $this->queuedInserts) {
|
2009-05-28 15:33:50 +04:00
|
|
|
return;
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
$postInsertIds = array();
|
|
|
|
$idGenerator = $this->class->idGenerator;
|
|
|
|
$isPostInsertId = $idGenerator->isPostInsertGenerator();
|
|
|
|
$rootClass = ($this->class->name !== $this->class->rootEntityName)
|
|
|
|
? $this->em->getClassMetadata($this->class->rootEntityName)
|
|
|
|
: $this->class;
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-12-18 15:30:19 +03:00
|
|
|
// Prepare statement for the root table
|
2012-07-22 20:42:43 +04:00
|
|
|
$rootPersister = $this->em->getUnitOfWork()->getEntityPersister($rootClass->name);
|
2011-09-07 08:48:19 +04:00
|
|
|
$rootTableName = $rootClass->getTableName();
|
2012-07-22 20:42:43 +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
|
|
|
|
2012-07-22 20:42:43 +04:00
|
|
|
if ($rootClass !== $this->class) {
|
|
|
|
$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
|
|
|
|
2012-07-22 20:42:43 +04: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) {
|
2012-07-22 20:42:43 +04:00
|
|
|
$parentPersister = $this->em->getUnitOfWork()->getEntityPersister($parentClassName);
|
|
|
|
$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
|
2012-07-22 20:42:43 +04:00
|
|
|
foreach ($this->queuedInserts as $entity) {
|
|
|
|
$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) {
|
2012-07-22 20:42:43 +04:00
|
|
|
$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) {
|
2012-07-26 05:18:25 +04:00
|
|
|
$id = $idGenerator->generate($this->em, $entity);
|
2009-05-28 15:33:50 +04:00
|
|
|
$postInsertIds[$id] = $entity;
|
|
|
|
} else {
|
2012-07-22 20:42:43 +04:00
|
|
|
$id = $this->em->getUnitOfWork()->getEntityIdentifier($entity);
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
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) {
|
2012-12-16 21:20:10 +04:00
|
|
|
/** @var \Doctrine\DBAL\Statement $stmt */
|
2009-05-28 15:33:50 +04:00
|
|
|
$paramIndex = 1;
|
2012-07-26 05:18:25 +04:00
|
|
|
$data = isset($insertData[$tableName])
|
|
|
|
? $insertData[$tableName]
|
|
|
|
: array();
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2011-09-05 09:27:06 +04:00
|
|
|
foreach ((array) $id as $idName => $idVal) {
|
2012-07-22 20:42:43 +04:00
|
|
|
$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) {
|
2012-12-16 21:20:10 +04:00
|
|
|
if (!isset($id[$columnName])) {
|
|
|
|
$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
|
|
|
|
2012-07-22 20:42:43 +04:00
|
|
|
if ($this->class->isVersioned) {
|
2011-01-23 14:48:28 +03:00
|
|
|
$this->assignDefaultVersionValue($entity, $id);
|
2009-07-17 22:13:03 +04:00
|
|
|
}
|
|
|
|
|
2012-07-22 20:42:43 +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)
|
|
|
|
{
|
2012-07-22 20:42:43 +04:00
|
|
|
$updateData = $this->prepareUpdateData($entity);
|
2008-05-18 00:04:56 +04:00
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
if ( ! $updateData) {
|
|
|
|
return;
|
2009-07-18 01:55:56 +04:00
|
|
|
}
|
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
if (($isVersioned = $this->class->isVersioned) === false) {
|
|
|
|
return;
|
|
|
|
}
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
$versionedClass = $this->getVersionedClassMetadata();
|
|
|
|
$versionedTable = $versionedClass->getTableName();
|
2010-11-10 01:15:14 +03:00
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
foreach ($updateData as $tableName => $data) {
|
|
|
|
$tableName = $this->quotedTableMap[$tableName];
|
|
|
|
$versioned = $isVersioned && $versionedTable === $tableName;
|
|
|
|
|
|
|
|
$this->updateTable($entity, $tableName, $data, $versioned);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the table with the version column is updated even if no columns on that
|
|
|
|
// table were affected.
|
2012-12-24 13:38:49 +04:00
|
|
|
if ($isVersioned) {
|
|
|
|
if ( ! isset($updateData[$versionedTable])) {
|
|
|
|
$tableName = $this->quoteStrategy->getTableName($versionedClass, $this->platform);
|
|
|
|
$this->updateTable($entity, $tableName, array(), true);
|
|
|
|
}
|
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
$identifiers = $this->em->getUnitOfWork()->getEntityIdentifier($entity);
|
|
|
|
$this->assignDefaultVersionValue($entity, $identifiers);
|
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)
|
|
|
|
{
|
2012-07-22 20:42:43 +04:00
|
|
|
$identifier = $this->em->getUnitOfWork()->getEntityIdentifier($entity);
|
2012-07-26 05:18:25 +04:00
|
|
|
$id = array_combine($this->class->getIdentifierColumnNames(), $identifier);
|
2010-07-10 00:55:30 +04:00
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
$this->deleteJoinTableRecords($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.
|
2012-07-22 20:42:43 +04:00
|
|
|
if ($this->platform->supportsForeignKeyConstraints()) {
|
2012-07-26 05:18:25 +04:00
|
|
|
$rootClass = $this->em->getClassMetadata($this->class->rootEntityName);
|
|
|
|
$rootTable = $this->quoteStrategy->getTableName($rootClass, $this->platform);
|
|
|
|
|
|
|
|
$this->conn->delete($rootTable, $id);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete from all tables individually, starting from this class' table up to the root table.
|
|
|
|
$rootTable = $this->quoteStrategy->getTableName($this->class, $this->platform);
|
|
|
|
|
|
|
|
$this->conn->delete($rootTable, $id);
|
|
|
|
|
|
|
|
foreach ($this->class->parentClasses as $parentClass) {
|
|
|
|
$parentMetadata = $this->em->getClassMetadata($parentClass);
|
|
|
|
$parentTable = $this->quoteStrategy->getTableName($parentMetadata, $this->platform);
|
|
|
|
|
|
|
|
$this->conn->delete($parentTable, $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
|
|
|
*/
|
2012-07-25 05:33:23 +04:00
|
|
|
protected function getSelectSQL($criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null)
|
2009-05-28 15:33:50 +04:00
|
|
|
{
|
2012-07-26 05:18:25 +04:00
|
|
|
$joinSql = '';
|
|
|
|
$identifierColumn = $this->class->getIdentifierColumnNames();
|
|
|
|
$baseTableAlias = $this->getSQLTableAlias($this->class->name);
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
// INNER JOIN parent tables
|
2012-07-22 20:42:43 +04:00
|
|
|
foreach ($this->class->parentClasses as $parentClassName) {
|
2013-03-11 04:08:58 +04:00
|
|
|
$conditions = array();
|
2012-07-26 05:18:25 +04:00
|
|
|
$parentClass = $this->em->getClassMetadata($parentClassName);
|
|
|
|
$tableAlias = $this->getSQLTableAlias($parentClassName);
|
|
|
|
$joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON ';
|
2011-10-28 02:50:10 +04:00
|
|
|
|
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
foreach ($identifierColumn as $idColumn) {
|
2013-03-11 04:08:58 +04:00
|
|
|
$conditions[] = $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
2012-07-26 05:18:25 +04:00
|
|
|
|
2013-03-11 04:08:58 +04:00
|
|
|
$joinSql .= implode(' AND ', $conditions);
|
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
|
2012-07-22 20:42:43 +04:00
|
|
|
foreach ($this->class->subClasses as $subClassName) {
|
2013-03-11 04:08:58 +04:00
|
|
|
$conditions = array();
|
2012-07-26 05:18:25 +04:00
|
|
|
$subClass = $this->em->getClassMetadata($subClassName);
|
|
|
|
$tableAlias = $this->getSQLTableAlias($subClassName);
|
|
|
|
$joinSql .= ' LEFT JOIN ' . $this->quoteStrategy->getTableName($subClass, $this->platform) . ' ' . $tableAlias . ' ON ';
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
foreach ($identifierColumn as $idColumn) {
|
2013-03-11 04:08:58 +04:00
|
|
|
$conditions[] = $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
2009-10-22 23:12:00 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2013-03-11 04:08:58 +04:00
|
|
|
$joinSql .= implode(' AND ', $conditions);
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
if ($assoc != null && $assoc['type'] == ClassMetadata::MANY_TO_MANY) {
|
|
|
|
$joinSql .= $this->getSelectManyToManyJoinSQL($assoc);
|
|
|
|
}
|
2010-04-26 15:02:30 +04:00
|
|
|
|
2012-06-19 02:01:03 +04:00
|
|
|
$conditionSql = ($criteria instanceof Criteria)
|
2012-07-22 20:42:43 +04:00
|
|
|
? $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
|
2012-07-22 20:42:43 +04:00
|
|
|
if ($filterSql = $this->generateFilterConditionSQL($this->em->getClassMetadata($this->class->rootEntityName), $this->getSQLTableAlias($this->class->rootEntityName))) {
|
2012-07-26 05:18:25 +04:00
|
|
|
$conditionSql .= $conditionSql
|
|
|
|
? ' AND ' . $filterSql
|
|
|
|
: $filterSql;
|
2011-12-05 21:26:56 +04:00
|
|
|
}
|
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
$orderBySql = '';
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
if ($assoc !== null && isset($assoc['orderBy'])) {
|
|
|
|
$orderBy = $assoc['orderBy'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($orderBy) {
|
|
|
|
$orderBySql = $this->getOrderBySQL($orderBy, $baseTableAlias);
|
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
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
switch ($lockMode) {
|
|
|
|
case LockMode::PESSIMISTIC_READ:
|
|
|
|
|
|
|
|
$lockSql = ' ' . $this->platform->getReadLockSql();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LockMode::PESSIMISTIC_WRITE:
|
|
|
|
|
|
|
|
$lockSql = ' ' . $this->platform->getWriteLockSql();
|
|
|
|
|
|
|
|
break;
|
2010-12-04 21:44:10 +03:00
|
|
|
}
|
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
$tableName = $this->quoteStrategy->getTableName($this->class, $this->platform);
|
|
|
|
$where = $conditionSql != '' ? ' WHERE ' . $conditionSql : '';
|
|
|
|
$columnList = $this->getSelectColumnsSQL();
|
|
|
|
$query = 'SELECT ' . $columnList
|
|
|
|
. ' FROM '
|
|
|
|
. $tableName . ' ' . $baseTableAlias
|
|
|
|
. $joinSql
|
|
|
|
. $where
|
|
|
|
. $orderBySql;
|
|
|
|
|
|
|
|
return $this->platform->modifyLimitQuery($query, $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
|
|
|
{
|
2012-07-26 05:18:25 +04:00
|
|
|
$joinSql = '';
|
|
|
|
$identifierColumns = $this->class->getIdentifierColumnNames();
|
|
|
|
$baseTableAlias = $this->getSQLTableAlias($this->class->name);
|
|
|
|
$quotedTableName = $this->quoteStrategy->getTableName($this->class, $this->platform);
|
2010-07-04 16:37:17 +04:00
|
|
|
|
|
|
|
// INNER JOIN parent tables
|
2012-07-22 20:42:43 +04:00
|
|
|
foreach ($this->class->parentClasses as $parentClassName) {
|
2012-07-26 05:18:25 +04:00
|
|
|
$conditions = array();
|
|
|
|
$tableAlias = $this->getSQLTableAlias($parentClassName);
|
|
|
|
$parentClass = $this->em->getClassMetadata($parentClassName);
|
|
|
|
$joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON ';
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
foreach ($identifierColumns as $idColumn) {
|
|
|
|
$conditions[] = $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
2010-07-04 16:37:17 +04:00
|
|
|
}
|
2012-07-26 05:18:25 +04:00
|
|
|
|
|
|
|
$joinSql .= implode(' AND ', $conditions);
|
2010-07-04 16:37:17 +04:00
|
|
|
}
|
|
|
|
|
2012-07-26 05:18:25 +04:00
|
|
|
return 'FROM ' . $quotedTableName . ' ' . $baseTableAlias . $joinSql;
|
2010-05-02 15:02:44 +04:00
|
|
|
}
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2012-12-03 14:15:43 +04:00
|
|
|
/**
|
2012-07-22 22:26:00 +04:00
|
|
|
* Ensure this method is never called. This persister overrides getSelectEntitiesSQL directly.
|
2012-12-03 14:15:43 +04:00
|
|
|
*
|
|
|
|
* @return string
|
2012-07-22 22:26:00 +04:00
|
|
|
*/
|
2012-07-25 05:33:23 +04:00
|
|
|
protected function getSelectColumnsSQL()
|
2010-03-05 19:35:00 +03:00
|
|
|
{
|
2012-07-26 05:18:25 +04:00
|
|
|
// Create the column list fragment only once
|
|
|
|
if ($this->selectColumnListSql !== null) {
|
|
|
|
return $this->selectColumnListSql;
|
|
|
|
}
|
|
|
|
|
|
|
|
$columnList = array();
|
|
|
|
$this->rsm = new ResultSetMapping();
|
|
|
|
$discrColumn = $this->class->discriminatorColumn['name'];
|
|
|
|
$baseTableAlias = $this->getSQLTableAlias($this->class->name);
|
|
|
|
$resultColumnName = $this->platform->getSQLResultCasing($discrColumn);
|
|
|
|
|
|
|
|
$this->rsm->addEntityResult($this->class->name, 'r');
|
|
|
|
$this->rsm->setDiscriminatorColumn('r', $resultColumnName);
|
|
|
|
$this->rsm->addMetaResult('r', $resultColumnName, $discrColumn);
|
|
|
|
|
|
|
|
// Add regular columns
|
|
|
|
foreach ($this->class->fieldMappings as $fieldName => $mapping) {
|
|
|
|
$class = isset($mapping['inherited'])
|
|
|
|
? $this->em->getClassMetadata($mapping['inherited'])
|
|
|
|
: $this->class;
|
|
|
|
|
|
|
|
$columnList[] = $this->getSelectColumnSQL($fieldName, $class);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add foreign key columns
|
|
|
|
foreach ($this->class->associationMappings as $mapping) {
|
|
|
|
if ( ! $mapping['isOwningSide'] || ! ($mapping['type'] & ClassMetadata::TO_ONE)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tableAlias = isset($mapping['inherited'])
|
|
|
|
? $this->getSQLTableAlias($mapping['inherited'])
|
|
|
|
: $baseTableAlias;
|
|
|
|
|
|
|
|
foreach ($mapping['targetToSourceKeyColumns'] as $srcColumn) {
|
|
|
|
$className = isset($mapping['inherited'])
|
|
|
|
? $mapping['inherited']
|
|
|
|
: $this->class->name;
|
|
|
|
|
|
|
|
$columnList[] = $this->getSelectJoinColumnSQL($tableAlias, $srcColumn, $className);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add discriminator column (DO NOT ALIAS, see AbstractEntityInheritancePersister#processSQLResult).
|
|
|
|
$tableAlias = ($this->class->rootEntityName == $this->class->name)
|
|
|
|
? $baseTableAlias
|
|
|
|
: $this->getSQLTableAlias($this->class->rootEntityName);
|
|
|
|
|
|
|
|
$columnList[] = $tableAlias . '.' . $discrColumn;
|
|
|
|
|
|
|
|
// sub tables
|
|
|
|
foreach ($this->class->subClasses as $subClassName) {
|
|
|
|
$subClass = $this->em->getClassMetadata($subClassName);
|
|
|
|
$tableAlias = $this->getSQLTableAlias($subClassName);
|
|
|
|
|
|
|
|
// Add subclass columns
|
|
|
|
foreach ($subClass->fieldMappings as $fieldName => $mapping) {
|
|
|
|
if (isset($mapping['inherited'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$columnList[] = $this->getSelectColumnSQL($fieldName, $subClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add join columns (foreign keys)
|
|
|
|
foreach ($subClass->associationMappings as $mapping) {
|
|
|
|
if ( ! $mapping['isOwningSide']
|
|
|
|
|| ! ($mapping['type'] & ClassMetadata::TO_ONE)
|
|
|
|
|| isset($mapping['inherited'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($mapping['targetToSourceKeyColumns'] as $srcColumn) {
|
|
|
|
$className = isset($mapping['inherited'])
|
|
|
|
? $mapping['inherited']
|
|
|
|
: $subClass->name;
|
|
|
|
|
|
|
|
$columnList[] = $this->getSelectJoinColumnSQL($tableAlias, $srcColumn, $className);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->selectColumnListSql = implode(', ', $columnList);
|
|
|
|
|
|
|
|
return $this->selectColumnListSql;
|
2010-03-05 19:35:00 +03:00
|
|
|
}
|
2011-10-28 02:50:10 +04:00
|
|
|
|
2012-07-22 22:26:00 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-07-22 20:42:43 +04:00
|
|
|
protected function getInsertColumnList()
|
2009-12-17 16:37:47 +03:00
|
|
|
{
|
|
|
|
// Identifier columns must always come first in the column list of subclasses.
|
2012-07-26 05:18:25 +04:00
|
|
|
$columns = $this->class->parentClasses
|
|
|
|
? $this->class->getIdentifierColumnNames()
|
|
|
|
: array();
|
2009-12-17 16:37:47 +03:00
|
|
|
|
2012-07-22 20:42:43 +04:00
|
|
|
foreach ($this->class->reflFields as $name => $field) {
|
2012-07-26 05:18:25 +04:00
|
|
|
if (isset($this->class->fieldMappings[$name]['inherited'])
|
|
|
|
&& ! isset($this->class->fieldMappings[$name]['id'])
|
2012-07-22 20:42:43 +04:00
|
|
|
|| isset($this->class->associationMappings[$name]['inherited'])
|
|
|
|
|| ($this->class->isVersioned && $this->class->versionField == $name)) {
|
2009-12-17 16:37:47 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-07-22 20:42:43 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2012-07-22 20:42:43 +04:00
|
|
|
} else if ($this->class->name != $this->class->rootEntityName ||
|
|
|
|
! $this->class->isIdGeneratorIdentity() || $this->class->identifier[0] != $name) {
|
|
|
|
$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.
|
2012-07-22 20:42:43 +04:00
|
|
|
if ($this->class->name == $this->class->rootEntityName) {
|
|
|
|
$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)
|
|
|
|
{
|
2012-07-22 20:42:43 +04:00
|
|
|
$value = $this->fetchVersionValue($this->getVersionedClassMetadata(), $id);
|
|
|
|
$this->class->setFieldValue($entity, $this->class->versionField, $value);
|
2011-01-23 14:48:28 +03:00
|
|
|
}
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|