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-05-14 14:03:09 +04:00
|
|
|
use Doctrine\Common\DoctrineException;
|
|
|
|
|
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
|
2008-05-18 00:04:56 +04:00
|
|
|
* database as it is defined by <tt>Class Table Inheritance</tt>.
|
|
|
|
*
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @version $Revision$
|
2009-02-02 14:55:50 +03:00
|
|
|
* @link www.doctrine-project.org
|
2008-05-18 00:04:56 +04:00
|
|
|
* @since 2.0
|
2009-05-03 15:49:48 +04:00
|
|
|
* @todo Reimplement.
|
2008-05-18 00:04:56 +04:00
|
|
|
*/
|
2009-05-21 12:53:40 +04:00
|
|
|
class JoinedSubclassPersister extends StandardEntityPersister
|
|
|
|
{
|
2009-05-28 15:33:50 +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.
|
|
|
|
*/
|
|
|
|
private $_owningTableMap = array();
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
protected function _prepareData($entity, array &$result, $isInsert = false)
|
|
|
|
{
|
|
|
|
parent::_prepareData($entity, $result, $isInsert);
|
|
|
|
// Populate the discriminator column
|
|
|
|
if ($isInsert) {
|
|
|
|
$discColumn = $this->_class->discriminatorColumn;
|
|
|
|
$rootClass = $this->_em->getClassMetadata($this->_class->rootEntityName);
|
|
|
|
$result[$rootClass->primaryTable['name']][$discColumn['name']] =
|
|
|
|
$this->_class->discriminatorValue;
|
|
|
|
}
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-07-18 01:55:56 +04:00
|
|
|
/**
|
|
|
|
* This function finds the ClassMetadata instance in a inheritance hierarchy
|
|
|
|
* that is responsible for enabling versioning.
|
|
|
|
*
|
|
|
|
* @return mixed $versionedClass ClassMetadata instance or false if versioning is not enabled
|
|
|
|
*/
|
|
|
|
private function _getVersionedClassMetadata()
|
|
|
|
{
|
|
|
|
if ($isVersioned = $this->_class->isVersioned) {
|
|
|
|
if (isset($this->_class->fieldMappings[$this->_class->versionField]['inherited'])) {
|
|
|
|
$definingClassName = $this->_class->fieldMappings[$this->_class->versionField]['inherited'];
|
|
|
|
$versionedClass = $this->_em->getClassMetadata($definingClassName);
|
|
|
|
} else {
|
|
|
|
$versionedClass = $this->_class;
|
|
|
|
}
|
|
|
|
return $versionedClass;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @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(
|
|
|
|
$this->_class->inheritedAssociationFields[$fieldName])->primaryTable['name'];
|
|
|
|
} else {
|
|
|
|
$this->_owningTableMap[$fieldName] = $this->_class->primaryTable['name'];
|
|
|
|
}
|
|
|
|
} else if (isset($this->_class->fieldMappings[$fieldName]['inherited'])) {
|
|
|
|
$this->_owningTableMap[$fieldName] = $this->_em->getClassMetadata(
|
|
|
|
$this->_class->fieldMappings[$fieldName]['inherited'])->primaryTable['name'];
|
|
|
|
} else {
|
|
|
|
$this->_owningTableMap[$fieldName] = $this->_class->primaryTable['name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->_owningTableMap[$fieldName];
|
|
|
|
}
|
2008-05-18 00:04:56 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function executeInserts()
|
|
|
|
{
|
|
|
|
if ( ! $this->_queuedInserts) {
|
|
|
|
return;
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-07-17 22:13:03 +04:00
|
|
|
if ($isVersioned = $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();
|
|
|
|
$sqlLogger = $this->_conn->getConfiguration()->getSqlLogger();
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
// Prepare statements for all tables
|
|
|
|
$stmts = $classes = array();
|
|
|
|
$stmts[$this->_class->primaryTable['name']] = $this->_conn->prepare($this->_class->insertSql);
|
|
|
|
$sql[$this->_class->primaryTable['name']] = $this->_class->insertSql;
|
|
|
|
foreach ($this->_class->parentClasses as $parentClass) {
|
|
|
|
$parentClass = $this->_em->getClassMetadata($parentClass);
|
|
|
|
$sql[$parentClass->primaryTable['name']] = $parentClass->insertSql;
|
|
|
|
$stmts[$parentClass->primaryTable['name']] = $this->_conn->prepare($parentClass->insertSql);
|
|
|
|
}
|
|
|
|
$rootTableName = $this->_em->getClassMetadata($this->_class->rootEntityName)->primaryTable['name'];
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
foreach ($this->_queuedInserts as $entity) {
|
|
|
|
$insertData = array();
|
|
|
|
$this->_prepareData($entity, $insertData, true);
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
// Execute insert on root table
|
|
|
|
$stmt = $stmts[$rootTableName];
|
|
|
|
$paramIndex = 1;
|
|
|
|
if ($sqlLogger) {
|
|
|
|
$params = array();
|
|
|
|
foreach ($insertData[$rootTableName] as $columnName => $value) {
|
|
|
|
$params[$paramIndex] = $value;
|
|
|
|
$stmt->bindValue($paramIndex++, $value/*, TODO: TYPE*/);
|
|
|
|
}
|
|
|
|
$sqlLogger->logSql($sql[$rootTableName], $params);
|
|
|
|
} else {
|
|
|
|
foreach ($insertData[$rootTableName] as $columnName => $value) {
|
|
|
|
$stmt->bindValue($paramIndex++, $value/*, TODO: TYPE*/);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$stmt->execute();
|
|
|
|
unset($insertData[$rootTableName]);
|
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-05-28 15:33:50 +04:00
|
|
|
// Execute inserts on subtables
|
|
|
|
foreach ($insertData as $tableName => $data) {
|
|
|
|
$stmt = $stmts[$tableName];
|
|
|
|
$paramIndex = 1;
|
|
|
|
if ($sqlLogger) {
|
|
|
|
//TODO: Log type
|
|
|
|
$params = array();
|
|
|
|
foreach ((array)$id as $idVal) {
|
|
|
|
$params[$paramIndex] = $idVal;
|
|
|
|
$stmt->bindValue($paramIndex++, $idVal/*, TODO: TYPE*/);
|
|
|
|
}
|
|
|
|
foreach ($data as $columnName => $value) {
|
|
|
|
$params[$paramIndex] = $value;
|
|
|
|
$stmt->bindValue($paramIndex++, $value/*, TODO: TYPE*/);
|
|
|
|
}
|
|
|
|
$sqlLogger->logSql($sql[$tableName], $params);
|
|
|
|
} else {
|
|
|
|
foreach ((array)$id as $idVal) {
|
|
|
|
$stmt->bindValue($paramIndex++, $idVal/*, TODO: TYPE*/);
|
|
|
|
}
|
|
|
|
foreach ($data as $columnName => $value) {
|
|
|
|
$stmt->bindValue($paramIndex++, $value/*, TODO: TYPE*/);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$stmt->execute();
|
|
|
|
}
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
foreach ($stmts as $stmt)
|
|
|
|
$stmt->closeCursor();
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-07-17 22:13:03 +04:00
|
|
|
if ($isVersioned) {
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* Updates an entity.
|
|
|
|
*
|
|
|
|
* @param object $entity The entity to update.
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function update($entity)
|
|
|
|
{
|
|
|
|
$updateData = array();
|
|
|
|
$this->_prepareData($entity, $updateData);
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
$id = array_combine(
|
2009-07-03 21:36:41 +04:00
|
|
|
$this->_class->getIdentifierFieldNames(),
|
|
|
|
$this->_em->getUnitOfWork()->getEntityIdentifier($entity)
|
2009-05-28 15:33:50 +04:00
|
|
|
);
|
2008-05-18 00:04:56 +04:00
|
|
|
|
2009-07-18 01:55:56 +04:00
|
|
|
if ($isVersioned = $this->_class->isVersioned) {
|
|
|
|
$versionedClass = $this->_getVersionedClassMetadata();
|
|
|
|
$versionedTable = $versionedClass->primaryTable['name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($updateData) {
|
|
|
|
foreach ($updateData as $tableName => $data) {
|
|
|
|
if ($isVersioned && $versionedTable == $tableName) {
|
|
|
|
$this->_doUpdate($entity, $tableName, $data, $id);
|
|
|
|
} else {
|
|
|
|
$this->_conn->update($tableName, $data, $id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($isVersioned && ! isset($updateData[$versionedTable])) {
|
|
|
|
$this->_doUpdate($entity, $versionedTable, array(), $id);
|
|
|
|
}
|
2009-05-28 15:33:50 +04:00
|
|
|
}
|
|
|
|
}
|
2008-05-18 00:04:56 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
/**
|
|
|
|
* Deletes an entity.
|
|
|
|
*
|
|
|
|
* @param object $entity The entity to delete.
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function delete($entity)
|
|
|
|
{
|
|
|
|
$id = array_combine(
|
2009-07-03 21:36:41 +04:00
|
|
|
$this->_class->getIdentifierFieldNames(),
|
|
|
|
$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.
|
|
|
|
if ($this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) {
|
|
|
|
$this->_conn->delete($this->_em->getClassMetadata($this->_class->rootEntityName)
|
2009-07-03 21:36:41 +04:00
|
|
|
->primaryTable['name'], $id);
|
2009-05-28 15:33:50 +04:00
|
|
|
} else {
|
|
|
|
// Delete the parent tables, starting from this class' table up to the root table
|
|
|
|
$this->_conn->delete($this->_class->primaryTable['name'], $id);
|
|
|
|
foreach ($this->_class->parentClasses as $parentClass) {
|
|
|
|
$this->_conn->delete($this->_em->getClassMetadata($parentClass)->primaryTable['name'], $id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
/**
|
|
|
|
* Gets the SELECT SQL to select a single entity by a set of field criteria.
|
|
|
|
*
|
|
|
|
* @param array $criteria
|
|
|
|
* @return string The SQL.
|
|
|
|
* @todo Quote identifier.
|
|
|
|
* @override
|
|
|
|
*/
|
2009-07-28 20:36:24 +04:00
|
|
|
protected function _getSelectEntitiesSql(array &$criteria)
|
2009-05-28 15:33:50 +04:00
|
|
|
{
|
|
|
|
$tableAliases = array();
|
|
|
|
$aliasIndex = 1;
|
|
|
|
$idColumns = $this->_class->getIdentifierColumnNames();
|
|
|
|
$baseTableAlias = 't0';
|
|
|
|
|
|
|
|
foreach (array_merge($this->_class->subClasses, $this->_class->parentClasses) as $className) {
|
|
|
|
$tableAliases[$className] = 't' . $aliasIndex++;
|
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
$columnList = '';
|
|
|
|
foreach ($this->_class->fieldMappings as $fieldName => $mapping) {
|
|
|
|
$tableAlias = isset($mapping['inherited']) ?
|
|
|
|
$tableAliases[$mapping['inherited']] : $baseTableAlias;
|
|
|
|
if ($columnList != '') $columnList .= ', ';
|
|
|
|
$columnList .= $tableAlias . '.' . $this->_class->columnNames[$fieldName];
|
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
$sql = 'SELECT ' . $columnList . ' FROM ' . $this->_class->primaryTable['name']. ' ' . $baseTableAlias;
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
// INNER JOIN parent tables
|
|
|
|
foreach ($this->_class->parentClasses as $parentClassName) {
|
|
|
|
$parentClass = $this->_em->getClassMetadata($parentClassName);
|
|
|
|
$tableAlias = $tableAliases[$parentClassName];
|
|
|
|
$sql .= ' INNER JOIN ' . $parentClass->primaryTable['name'] . ' ' . $tableAlias . ' ON ';
|
|
|
|
$first = true;
|
|
|
|
foreach ($idColumns as $idColumn) {
|
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
|
|
|
$sql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
$tableAlias = $tableAliases[$subClassName];
|
|
|
|
$sql .= ' LEFT JOIN ' . $subClass->primaryTable['name'] . ' ' . $tableAlias . ' ON ';
|
|
|
|
$first = true;
|
|
|
|
foreach ($idColumns as $idColumn) {
|
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
|
|
|
$sql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
|
|
|
}
|
|
|
|
}
|
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 ';
|
|
|
|
$conditionSql .= $baseTableAlias . '.' . $this->_class->columnNames[$field] . ' = ?';
|
|
|
|
}
|
2009-05-28 15:13:12 +04:00
|
|
|
|
2009-05-28 15:33:50 +04:00
|
|
|
return $sql . ' WHERE ' . $conditionSql;
|
|
|
|
}
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|