Merge pull request #413 from FabioBatSilva/refactory-persisters
code refactorings on persister
This commit is contained in:
commit
05e5ae8bfa
@ -33,17 +33,17 @@ abstract class AbstractCollectionPersister
|
||||
/**
|
||||
* @var EntityManager
|
||||
*/
|
||||
protected $_em;
|
||||
protected $em;
|
||||
|
||||
/**
|
||||
* @var \Doctrine\DBAL\Connection
|
||||
*/
|
||||
protected $_conn;
|
||||
protected $conn;
|
||||
|
||||
/**
|
||||
* @var \Doctrine\ORM\UnitOfWork
|
||||
*/
|
||||
protected $_uow;
|
||||
protected $uow;
|
||||
|
||||
/**
|
||||
* The database platform.
|
||||
@ -66,17 +66,17 @@ abstract class AbstractCollectionPersister
|
||||
*/
|
||||
public function __construct(EntityManager $em)
|
||||
{
|
||||
$this->_em = $em;
|
||||
$this->_uow = $em->getUnitOfWork();
|
||||
$this->_conn = $em->getConnection();
|
||||
$this->platform = $this->_conn->getDatabasePlatform();
|
||||
$this->em = $em;
|
||||
$this->uow = $em->getUnitOfWork();
|
||||
$this->conn = $em->getConnection();
|
||||
$this->platform = $this->conn->getDatabasePlatform();
|
||||
$this->quoteStrategy = $em->getConfiguration()->getQuoteStrategy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the persistent state represented by the given collection.
|
||||
*
|
||||
* @param PersistentCollection $coll
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
*/
|
||||
public function delete(PersistentCollection $coll)
|
||||
{
|
||||
@ -86,30 +86,31 @@ abstract class AbstractCollectionPersister
|
||||
return; // ignore inverse side
|
||||
}
|
||||
|
||||
$sql = $this->_getDeleteSQL($coll);
|
||||
$this->_conn->executeUpdate($sql, $this->_getDeleteSQLParameters($coll));
|
||||
$sql = $this->getDeleteSQL($coll);
|
||||
|
||||
$this->conn->executeUpdate($sql, $this->getDeleteSQLParameters($coll));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the SQL statement for deleting the given collection.
|
||||
*
|
||||
* @param PersistentCollection $coll
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
*/
|
||||
abstract protected function _getDeleteSQL(PersistentCollection $coll);
|
||||
abstract protected function getDeleteSQL(PersistentCollection $coll);
|
||||
|
||||
/**
|
||||
* Gets the SQL parameters for the corresponding SQL statement to delete
|
||||
* the given collection.
|
||||
*
|
||||
* @param PersistentCollection $coll
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
*/
|
||||
abstract protected function _getDeleteSQLParameters(PersistentCollection $coll);
|
||||
abstract protected function getDeleteSQLParameters(PersistentCollection $coll);
|
||||
|
||||
/**
|
||||
* Updates the given collection, synchronizing it's state with the database
|
||||
* by inserting, updating and deleting individual elements.
|
||||
*
|
||||
* @param PersistentCollection $coll
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
*/
|
||||
public function update(PersistentCollection $coll)
|
||||
{
|
||||
@ -120,63 +121,124 @@ abstract class AbstractCollectionPersister
|
||||
}
|
||||
|
||||
$this->deleteRows($coll);
|
||||
//$this->updateRows($coll);
|
||||
$this->insertRows($coll);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete rows
|
||||
*
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
*/
|
||||
public function deleteRows(PersistentCollection $coll)
|
||||
{
|
||||
$deleteDiff = $coll->getDeleteDiff();
|
||||
$sql = $this->_getDeleteRowSQL($coll);
|
||||
$diff = $coll->getDeleteDiff();
|
||||
$sql = $this->getDeleteRowSQL($coll);
|
||||
|
||||
foreach ($deleteDiff as $element) {
|
||||
$this->_conn->executeUpdate($sql, $this->_getDeleteRowSQLParameters($coll, $element));
|
||||
foreach ($diff as $element) {
|
||||
$this->conn->executeUpdate($sql, $this->getDeleteRowSQLParameters($coll, $element));
|
||||
}
|
||||
}
|
||||
|
||||
//public function updateRows(PersistentCollection $coll)
|
||||
//{}
|
||||
|
||||
/**
|
||||
* Insert rows
|
||||
*
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
*/
|
||||
public function insertRows(PersistentCollection $coll)
|
||||
{
|
||||
$insertDiff = $coll->getInsertDiff();
|
||||
$sql = $this->_getInsertRowSQL($coll);
|
||||
$diff = $coll->getInsertDiff();
|
||||
$sql = $this->getInsertRowSQL($coll);
|
||||
|
||||
foreach ($insertDiff as $element) {
|
||||
$this->_conn->executeUpdate($sql, $this->_getInsertRowSQLParameters($coll, $element));
|
||||
foreach ($diff as $element) {
|
||||
$this->conn->executeUpdate($sql, $this->getInsertRowSQLParameters($coll, $element));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the size of this persistent collection
|
||||
*
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function count(PersistentCollection $coll)
|
||||
{
|
||||
throw new \BadMethodCallException("Counting the size of this persistent collection is not supported by this CollectionPersister.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Slice elements
|
||||
*
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
* @param integer $offset
|
||||
* @param integer $length
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function slice(PersistentCollection $coll, $offset, $length = null)
|
||||
{
|
||||
throw new \BadMethodCallException("Slicing elements is not supported by this CollectionPersister.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for existance of an element
|
||||
*
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
* @param object $element
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function contains(PersistentCollection $coll, $element)
|
||||
{
|
||||
throw new \BadMethodCallException("Checking for existance of an element is not supported by this CollectionPersister.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for existance of a key
|
||||
*
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
* @param mixed $key
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function containsKey(PersistentCollection $coll, $key)
|
||||
{
|
||||
throw new \BadMethodCallException("Checking for existance of a key is not supported by this CollectionPersister.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an element
|
||||
*
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
* @param object $element
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function removeElement(PersistentCollection $coll, $element)
|
||||
{
|
||||
throw new \BadMethodCallException("Removing an element is not supported by this CollectionPersister.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an element by key
|
||||
*
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
*
|
||||
* @param mixed $key
|
||||
*/
|
||||
public function removeKey(PersistentCollection $coll, $key)
|
||||
{
|
||||
throw new \BadMethodCallException("Removing a key is not supported by this CollectionPersister.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an element by key
|
||||
*
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
* @param mixed $index
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(PersistentCollection $coll, $index)
|
||||
{
|
||||
throw new \BadMethodCallException("Selecting a collection by index is not supported by this CollectionPersister.");
|
||||
@ -185,39 +247,39 @@ abstract class AbstractCollectionPersister
|
||||
/**
|
||||
* Gets the SQL statement used for deleting a row from the collection.
|
||||
*
|
||||
* @param PersistentCollection $coll
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
*/
|
||||
abstract protected function _getDeleteRowSQL(PersistentCollection $coll);
|
||||
abstract protected function getDeleteRowSQL(PersistentCollection $coll);
|
||||
|
||||
/**
|
||||
* Gets the SQL parameters for the corresponding SQL statement to delete the given
|
||||
* element from the given collection.
|
||||
*
|
||||
* @param PersistentCollection $coll
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
* @param mixed $element
|
||||
*/
|
||||
abstract protected function _getDeleteRowSQLParameters(PersistentCollection $coll, $element);
|
||||
abstract protected function getDeleteRowSQLParameters(PersistentCollection $coll, $element);
|
||||
|
||||
/**
|
||||
* Gets the SQL statement used for updating a row in the collection.
|
||||
*
|
||||
* @param PersistentCollection $coll
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
*/
|
||||
abstract protected function _getUpdateRowSQL(PersistentCollection $coll);
|
||||
abstract protected function getUpdateRowSQL(PersistentCollection $coll);
|
||||
|
||||
/**
|
||||
* Gets the SQL statement used for inserting a row in the collection.
|
||||
*
|
||||
* @param PersistentCollection $coll
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
*/
|
||||
abstract protected function _getInsertRowSQL(PersistentCollection $coll);
|
||||
abstract protected function getInsertRowSQL(PersistentCollection $coll);
|
||||
|
||||
/**
|
||||
* Gets the SQL parameters for the corresponding SQL statement to insert the given
|
||||
* element of the given collection into the database.
|
||||
*
|
||||
* @param PersistentCollection $coll
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
* @param mixed $element
|
||||
*/
|
||||
abstract protected function _getInsertRowSQLParameters(PersistentCollection $coll, $element);
|
||||
abstract protected function getInsertRowSQLParameters(PersistentCollection $coll, $element);
|
||||
}
|
||||
|
@ -36,14 +36,14 @@ abstract class AbstractEntityInheritancePersister extends BasicEntityPersister
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function _prepareInsertData($entity)
|
||||
protected function prepareInsertData($entity)
|
||||
{
|
||||
$data = parent::_prepareInsertData($entity);
|
||||
$data = parent::prepareInsertData($entity);
|
||||
|
||||
// Populate the discriminator column
|
||||
$discColumn = $this->_class->discriminatorColumn;
|
||||
$this->_columnTypes[$discColumn['name']] = $discColumn['type'];
|
||||
$data[$this->_getDiscriminatorColumnTableName()][$discColumn['name']] = $this->_class->discriminatorValue;
|
||||
$discColumn = $this->class->discriminatorColumn;
|
||||
$this->columnTypes[$discColumn['name']] = $discColumn['type'];
|
||||
$data[$this->getDiscriminatorColumnTableName()][$discColumn['name']] = $this->class->discriminatorValue;
|
||||
|
||||
return $data;
|
||||
}
|
||||
@ -53,21 +53,24 @@ abstract class AbstractEntityInheritancePersister extends BasicEntityPersister
|
||||
*
|
||||
* @return string The table name.
|
||||
*/
|
||||
abstract protected function _getDiscriminatorColumnTableName();
|
||||
abstract protected function getDiscriminatorColumnTableName();
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function _getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r')
|
||||
protected function getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r')
|
||||
{
|
||||
$columnName = $class->columnNames[$field];
|
||||
$sql = $this->_getSQLTableAlias($class->name, $alias == 'r' ? '' : $alias) . '.' . $this->quoteStrategy->getColumnName($field, $class, $this->_platform);
|
||||
$tableAlias = $alias == 'r' ? '' : $alias;
|
||||
$columnName = $class->columnNames[$field];
|
||||
$columnAlias = $this->getSQLColumnAlias($columnName);
|
||||
$this->_rsm->addFieldResult($alias, $columnAlias, $field, $class->name);
|
||||
$sql = $this->getSQLTableAlias($class->name, $tableAlias) . '.'
|
||||
. $this->quoteStrategy->getColumnName($field, $class, $this->platform);
|
||||
|
||||
$this->rsm->addFieldResult($alias, $columnAlias, $field, $class->name);
|
||||
|
||||
if (isset($class->fieldMappings[$field]['requireSQLConversion'])) {
|
||||
$type = Type::getType($class->getTypeOfField($field));
|
||||
$sql = $type->convertToPHPValueSQL($sql, $this->_platform);
|
||||
$type = Type::getType($class->getTypeOfField($field));
|
||||
$sql = $type->convertToPHPValueSQL($sql, $this->platform);
|
||||
}
|
||||
|
||||
return $sql . ' AS ' . $columnAlias;
|
||||
@ -76,7 +79,7 @@ abstract class AbstractEntityInheritancePersister extends BasicEntityPersister
|
||||
protected function getSelectJoinColumnSQL($tableAlias, $joinColumnName, $className)
|
||||
{
|
||||
$columnAlias = $this->getSQLColumnAlias($joinColumnName);
|
||||
$this->_rsm->addMetaResult('r', $columnAlias, $joinColumnName);
|
||||
$this->rsm->addMetaResult('r', $columnAlias, $joinColumnName);
|
||||
|
||||
return $tableAlias . '.' . $joinColumnName . ' AS ' . $columnAlias;
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -26,5 +26,5 @@ namespace Doctrine\ORM\Persisters;
|
||||
*/
|
||||
abstract class ElementCollectionPersister extends AbstractCollectionPersister
|
||||
{
|
||||
//put your code here
|
||||
|
||||
}
|
||||
|
@ -45,23 +45,23 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_owningTableMap = array();
|
||||
private $owningTableMap = array();
|
||||
|
||||
/**
|
||||
* Map of table to quoted table names.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_quotedTableMap = array();
|
||||
private $quotedTableMap = array();
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function _getDiscriminatorColumnTableName()
|
||||
protected function getDiscriminatorColumnTableName()
|
||||
{
|
||||
$class = ($this->_class->name !== $this->_class->rootEntityName)
|
||||
? $this->_em->getClassMetadata($this->_class->rootEntityName)
|
||||
: $this->_class;
|
||||
$class = ($this->class->name !== $this->class->rootEntityName)
|
||||
? $this->em->getClassMetadata($this->class->rootEntityName)
|
||||
: $this->class;
|
||||
|
||||
return $class->getTableName();
|
||||
}
|
||||
@ -72,15 +72,15 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
|
||||
*
|
||||
* @return \Doctrine\ORM\Mapping\ClassMetadata
|
||||
*/
|
||||
private function _getVersionedClassMetadata()
|
||||
private function getVersionedClassMetadata()
|
||||
{
|
||||
if (isset($this->_class->fieldMappings[$this->_class->versionField]['inherited'])) {
|
||||
$definingClassName = $this->_class->fieldMappings[$this->_class->versionField]['inherited'];
|
||||
if (isset($this->class->fieldMappings[$this->class->versionField]['inherited'])) {
|
||||
$definingClassName = $this->class->fieldMappings[$this->class->versionField]['inherited'];
|
||||
|
||||
return $this->_em->getClassMetadata($definingClassName);
|
||||
return $this->em->getClassMetadata($definingClassName);
|
||||
}
|
||||
|
||||
return $this->_class;
|
||||
return $this->class;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -92,22 +92,29 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
|
||||
*/
|
||||
public function getOwningTable($fieldName)
|
||||
{
|
||||
if (isset($this->_owningTableMap[$fieldName])) {
|
||||
return $this->_owningTableMap[$fieldName];
|
||||
if (isset($this->owningTableMap[$fieldName])) {
|
||||
return $this->owningTableMap[$fieldName];
|
||||
}
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
$tableName = $cm->getTableName();
|
||||
$tableName = $cm->getTableName();
|
||||
$quotedTableName = $this->quoteStrategy->getTableName($cm, $this->platform);
|
||||
|
||||
$this->_owningTableMap[$fieldName] = $tableName;
|
||||
$this->_quotedTableMap[$tableName] = $this->quoteStrategy->getTableName($cm, $this->_platform);
|
||||
$this->owningTableMap[$fieldName] = $tableName;
|
||||
$this->quotedTableMap[$tableName] = $quotedTableName;
|
||||
|
||||
return $tableName;
|
||||
}
|
||||
@ -117,73 +124,77 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
|
||||
*/
|
||||
public function executeInserts()
|
||||
{
|
||||
if ( ! $this->_queuedInserts) {
|
||||
if ( ! $this->queuedInserts) {
|
||||
return;
|
||||
}
|
||||
|
||||
$postInsertIds = array();
|
||||
$idGen = $this->_class->idGenerator;
|
||||
$isPostInsertId = $idGen->isPostInsertGenerator();
|
||||
$postInsertIds = array();
|
||||
$idGenerator = $this->class->idGenerator;
|
||||
$isPostInsertId = $idGenerator->isPostInsertGenerator();
|
||||
$rootClass = ($this->class->name !== $this->class->rootEntityName)
|
||||
? $this->em->getClassMetadata($this->class->rootEntityName)
|
||||
: $this->class;
|
||||
|
||||
// Prepare statement for the root table
|
||||
$rootClass = ($this->_class->name !== $this->_class->rootEntityName) ? $this->_em->getClassMetadata($this->_class->rootEntityName) : $this->_class;
|
||||
$rootPersister = $this->_em->getUnitOfWork()->getEntityPersister($rootClass->name);
|
||||
$rootPersister = $this->em->getUnitOfWork()->getEntityPersister($rootClass->name);
|
||||
$rootTableName = $rootClass->getTableName();
|
||||
$rootTableStmt = $this->_conn->prepare($rootPersister->_getInsertSQL());
|
||||
$rootTableStmt = $this->conn->prepare($rootPersister->getInsertSQL());
|
||||
|
||||
// Prepare statements for sub tables.
|
||||
$subTableStmts = array();
|
||||
|
||||
if ($rootClass !== $this->_class) {
|
||||
$subTableStmts[$this->_class->getTableName()] = $this->_conn->prepare($this->_getInsertSQL());
|
||||
if ($rootClass !== $this->class) {
|
||||
$subTableStmts[$this->class->getTableName()] = $this->conn->prepare($this->getInsertSQL());
|
||||
}
|
||||
|
||||
foreach ($this->_class->parentClasses as $parentClassName) {
|
||||
$parentClass = $this->_em->getClassMetadata($parentClassName);
|
||||
foreach ($this->class->parentClasses as $parentClassName) {
|
||||
$parentClass = $this->em->getClassMetadata($parentClassName);
|
||||
$parentTableName = $parentClass->getTableName();
|
||||
|
||||
if ($parentClass !== $rootClass) {
|
||||
$parentPersister = $this->_em->getUnitOfWork()->getEntityPersister($parentClassName);
|
||||
$subTableStmts[$parentTableName] = $this->_conn->prepare($parentPersister->_getInsertSQL());
|
||||
$parentPersister = $this->em->getUnitOfWork()->getEntityPersister($parentClassName);
|
||||
$subTableStmts[$parentTableName] = $this->conn->prepare($parentPersister->getInsertSQL());
|
||||
}
|
||||
}
|
||||
|
||||
// Execute all inserts. For each entity:
|
||||
// 1) Insert on root table
|
||||
// 2) Insert on sub tables
|
||||
foreach ($this->_queuedInserts as $entity) {
|
||||
$insertData = $this->_prepareInsertData($entity);
|
||||
foreach ($this->queuedInserts as $entity) {
|
||||
$insertData = $this->prepareInsertData($entity);
|
||||
|
||||
// Execute insert on root table
|
||||
$paramIndex = 1;
|
||||
|
||||
foreach ($insertData[$rootTableName] as $columnName => $value) {
|
||||
$rootTableStmt->bindValue($paramIndex++, $value, $this->_columnTypes[$columnName]);
|
||||
$rootTableStmt->bindValue($paramIndex++, $value, $this->columnTypes[$columnName]);
|
||||
}
|
||||
|
||||
$rootTableStmt->execute();
|
||||
|
||||
if ($isPostInsertId) {
|
||||
$id = $idGen->generate($this->_em, $entity);
|
||||
$id = $idGenerator->generate($this->em, $entity);
|
||||
$postInsertIds[$id] = $entity;
|
||||
} else {
|
||||
$id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity);
|
||||
$id = $this->em->getUnitOfWork()->getEntityIdentifier($entity);
|
||||
}
|
||||
|
||||
// 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();
|
||||
$paramIndex = 1;
|
||||
$data = isset($insertData[$tableName])
|
||||
? $insertData[$tableName]
|
||||
: array();
|
||||
|
||||
foreach ((array) $id as $idName => $idVal) {
|
||||
$type = isset($this->_columnTypes[$idName]) ? $this->_columnTypes[$idName] : Type::STRING;
|
||||
$type = isset($this->columnTypes[$idName]) ? $this->columnTypes[$idName] : Type::STRING;
|
||||
|
||||
$stmt->bindValue($paramIndex++, $idVal, $type);
|
||||
}
|
||||
|
||||
foreach ($data as $columnName => $value) {
|
||||
$stmt->bindValue($paramIndex++, $value, $this->_columnTypes[$columnName]);
|
||||
$stmt->bindValue($paramIndex++, $value, $this->columnTypes[$columnName]);
|
||||
}
|
||||
|
||||
$stmt->execute();
|
||||
@ -196,11 +207,11 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
|
||||
$stmt->closeCursor();
|
||||
}
|
||||
|
||||
if ($this->_class->isVersioned) {
|
||||
if ($this->class->isVersioned) {
|
||||
$this->assignDefaultVersionValue($entity, $id);
|
||||
}
|
||||
|
||||
$this->_queuedInserts = array();
|
||||
$this->queuedInserts = array();
|
||||
|
||||
return $postInsertIds;
|
||||
}
|
||||
@ -210,28 +221,34 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
|
||||
*/
|
||||
public function update($entity)
|
||||
{
|
||||
$updateData = $this->_prepareUpdateData($entity);
|
||||
$updateData = $this->prepareUpdateData($entity);
|
||||
|
||||
if (($isVersioned = $this->_class->isVersioned) != false) {
|
||||
$versionedClass = $this->_getVersionedClassMetadata();
|
||||
$versionedTable = $versionedClass->getTableName();
|
||||
if ( ! $updateData) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($updateData) {
|
||||
foreach ($updateData as $tableName => $data) {
|
||||
$this->_updateTable(
|
||||
$entity, $this->_quotedTableMap[$tableName], $data, $isVersioned && $versionedTable == $tableName
|
||||
);
|
||||
}
|
||||
if (($isVersioned = $this->class->isVersioned) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure the table with the version column is updated even if no columns on that
|
||||
// table were affected.
|
||||
if ($isVersioned && ! isset($updateData[$versionedTable])) {
|
||||
$this->_updateTable($entity, $this->quoteStrategy->getTableName($versionedClass, $this->_platform), array(), true);
|
||||
$versionedClass = $this->getVersionedClassMetadata();
|
||||
$versionedTable = $versionedClass->getTableName();
|
||||
|
||||
$id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity);
|
||||
$this->assignDefaultVersionValue($entity, $id);
|
||||
}
|
||||
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.
|
||||
if ($isVersioned && ! isset($updateData[$versionedTable])) {
|
||||
$tableName = $this->quoteStrategy->getTableName($versionedClass, $this->platform);
|
||||
$identifiers = $this->em->getUnitOfWork()->getEntityIdentifier($entity);
|
||||
|
||||
$this->updateTable($entity, $tableName, array(), true);
|
||||
$this->assignDefaultVersionValue($entity, $identifiers);
|
||||
}
|
||||
}
|
||||
|
||||
@ -240,174 +257,126 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
|
||||
*/
|
||||
public function delete($entity)
|
||||
{
|
||||
$identifier = $this->_em->getUnitOfWork()->getEntityIdentifier($entity);
|
||||
$this->deleteJoinTableRecords($identifier);
|
||||
$identifier = $this->em->getUnitOfWork()->getEntityIdentifier($entity);
|
||||
$id = array_combine($this->class->getIdentifierColumnNames(), $identifier);
|
||||
|
||||
$id = array_combine($this->_class->getIdentifierColumnNames(), $identifier);
|
||||
$this->deleteJoinTableRecords($identifier);
|
||||
|
||||
// If the database platform supports FKs, just
|
||||
// delete the row from the root table. Cascades do the rest.
|
||||
if ($this->_platform->supportsForeignKeyConstraints()) {
|
||||
$this->_conn->delete(
|
||||
$this->quoteStrategy->getTableName($this->_em->getClassMetadata($this->_class->rootEntityName), $this->_platform), $id
|
||||
);
|
||||
} else {
|
||||
// Delete from all tables individually, starting from this class' table up to the root table.
|
||||
$this->_conn->delete($this->quoteStrategy->getTableName($this->_class, $this->_platform), $id);
|
||||
if ($this->platform->supportsForeignKeyConstraints()) {
|
||||
$rootClass = $this->em->getClassMetadata($this->class->rootEntityName);
|
||||
$rootTable = $this->quoteStrategy->getTableName($rootClass, $this->platform);
|
||||
|
||||
foreach ($this->_class->parentClasses as $parentClass) {
|
||||
$this->_conn->delete(
|
||||
$this->quoteStrategy->getTableName($this->_em->getClassMetadata($parentClass), $this->_platform), $id
|
||||
);
|
||||
}
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function _getSelectEntitiesSQL($criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null)
|
||||
protected function getSelectSQL($criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null)
|
||||
{
|
||||
$idColumns = $this->_class->getIdentifierColumnNames();
|
||||
$baseTableAlias = $this->_getSQLTableAlias($this->_class->name);
|
||||
$joinSql = '';
|
||||
$identifierColumn = $this->class->getIdentifierColumnNames();
|
||||
$baseTableAlias = $this->getSQLTableAlias($this->class->name);
|
||||
|
||||
// Create the column list fragment only once
|
||||
if ($this->_selectColumnListSql === null) {
|
||||
|
||||
$this->_rsm = new ResultSetMapping();
|
||||
$this->_rsm->addEntityResult($this->_class->name, 'r');
|
||||
|
||||
// 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
|
||||
);
|
||||
}
|
||||
|
||||
// Add foreign key columns
|
||||
foreach ($this->_class->associationMappings as $assoc2) {
|
||||
if ($assoc2['isOwningSide'] && $assoc2['type'] & ClassMetadata::TO_ONE) {
|
||||
$tableAlias = isset($assoc2['inherited']) ? $this->_getSQLTableAlias($assoc2['inherited']) : $baseTableAlias;
|
||||
|
||||
foreach ($assoc2['targetToSourceKeyColumns'] as $srcColumn) {
|
||||
if ($columnList != '') $columnList .= ', ';
|
||||
|
||||
$columnList .= $this->getSelectJoinColumnSQL(
|
||||
$tableAlias,
|
||||
$srcColumn,
|
||||
isset($assoc2['inherited']) ? $assoc2['inherited'] : $this->_class->name
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add discriminator column (DO NOT ALIAS, see AbstractEntityInheritancePersister#_processSQLResult).
|
||||
$discrColumn = $this->_class->discriminatorColumn['name'];
|
||||
$tableAlias = ($this->_class->rootEntityName == $this->_class->name) ? $baseTableAlias : $this->_getSQLTableAlias($this->_class->rootEntityName);
|
||||
$columnList .= ', ' . $tableAlias . '.' . $discrColumn;
|
||||
|
||||
$resultColumnName = $this->_platform->getSQLResultCasing($discrColumn);
|
||||
|
||||
$this->_rsm->setDiscriminatorColumn('r', $resultColumnName);
|
||||
$this->_rsm->addMetaResult('r', $resultColumnName, $discrColumn);
|
||||
}
|
||||
|
||||
// INNER JOIN parent tables
|
||||
$joinSql = '';
|
||||
foreach ($this->class->parentClasses as $parentClassName) {
|
||||
$contitions = array();
|
||||
$parentClass = $this->em->getClassMetadata($parentClassName);
|
||||
$tableAlias = $this->getSQLTableAlias($parentClassName);
|
||||
$joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON ';
|
||||
|
||||
foreach ($this->_class->parentClasses as $parentClassName) {
|
||||
$parentClass = $this->_em->getClassMetadata($parentClassName);
|
||||
$tableAlias = $this->_getSQLTableAlias($parentClassName);
|
||||
$joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->_platform) . ' ' . $tableAlias . ' ON ';
|
||||
$first = true;
|
||||
|
||||
foreach ($idColumns as $idColumn) {
|
||||
if ($first) $first = false; else $joinSql .= ' AND ';
|
||||
|
||||
$joinSql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
||||
foreach ($identifierColumn as $idColumn) {
|
||||
$contitions[] = $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
||||
}
|
||||
|
||||
$joinSql .= implode(' AND ', $contitions);
|
||||
}
|
||||
|
||||
// OUTER JOIN sub tables
|
||||
foreach ($this->_class->subClasses as $subClassName) {
|
||||
$subClass = $this->_em->getClassMetadata($subClassName);
|
||||
$tableAlias = $this->_getSQLTableAlias($subClassName);
|
||||
foreach ($this->class->subClasses as $subClassName) {
|
||||
$contitions = array();
|
||||
$subClass = $this->em->getClassMetadata($subClassName);
|
||||
$tableAlias = $this->getSQLTableAlias($subClassName);
|
||||
$joinSql .= ' LEFT JOIN ' . $this->quoteStrategy->getTableName($subClass, $this->platform) . ' ' . $tableAlias . ' ON ';
|
||||
|
||||
if ($this->_selectColumnListSql === null) {
|
||||
// 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 $assoc2) {
|
||||
if ($assoc2['isOwningSide'] && $assoc2['type'] & ClassMetadata::TO_ONE && ! isset($assoc2['inherited'])) {
|
||||
foreach ($assoc2['targetToSourceKeyColumns'] as $srcColumn) {
|
||||
if ($columnList != '') $columnList .= ', ';
|
||||
|
||||
$columnList .= $this->getSelectJoinColumnSQL(
|
||||
$tableAlias,
|
||||
$srcColumn,
|
||||
isset($assoc2['inherited']) ? $assoc2['inherited'] : $subClass->name
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($identifierColumn as $idColumn) {
|
||||
$contitions[] = $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
||||
}
|
||||
|
||||
// Add LEFT JOIN
|
||||
$joinSql .= ' LEFT JOIN ' . $this->quoteStrategy->getTableName($subClass, $this->_platform) . ' ' . $tableAlias . ' ON ';
|
||||
$first = true;
|
||||
|
||||
foreach ($idColumns as $idColumn) {
|
||||
if ($first) $first = false; else $joinSql .= ' AND ';
|
||||
|
||||
$joinSql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
||||
}
|
||||
$joinSql .= implode(' AND ', $contitions);
|
||||
}
|
||||
|
||||
$joinSql .= ($assoc != null && $assoc['type'] == ClassMetadata::MANY_TO_MANY) ? $this->_getSelectManyToManyJoinSQL($assoc) : '';
|
||||
if ($assoc != null && $assoc['type'] == ClassMetadata::MANY_TO_MANY) {
|
||||
$joinSql .= $this->getSelectManyToManyJoinSQL($assoc);
|
||||
}
|
||||
|
||||
$conditionSql = ($criteria instanceof Criteria)
|
||||
? $this->_getSelectConditionCriteriaSQL($criteria)
|
||||
: $this->_getSelectConditionSQL($criteria, $assoc);
|
||||
? $this->getSelectConditionCriteriaSQL($criteria)
|
||||
: $this->getSelectConditionSQL($criteria, $assoc);
|
||||
|
||||
// If the current class in the root entity, add the filters
|
||||
if ($filterSql = $this->generateFilterConditionSQL($this->_em->getClassMetadata($this->_class->rootEntityName), $this->_getSQLTableAlias($this->_class->rootEntityName))) {
|
||||
if ($conditionSql) {
|
||||
$conditionSql .= ' AND ';
|
||||
}
|
||||
|
||||
$conditionSql .= $filterSql;
|
||||
if ($filterSql = $this->generateFilterConditionSQL($this->em->getClassMetadata($this->class->rootEntityName), $this->getSQLTableAlias($this->class->rootEntityName))) {
|
||||
$conditionSql .= $conditionSql
|
||||
? ' AND ' . $filterSql
|
||||
: $filterSql;
|
||||
}
|
||||
|
||||
$orderBy = ($assoc !== null && isset($assoc['orderBy'])) ? $assoc['orderBy'] : $orderBy;
|
||||
$orderBySql = $orderBy ? $this->_getOrderBySQL($orderBy, $baseTableAlias) : '';
|
||||
$orderBySql = '';
|
||||
|
||||
if ($this->_selectColumnListSql === null) {
|
||||
$this->_selectColumnListSql = $columnList;
|
||||
if ($assoc !== null && isset($assoc['orderBy'])) {
|
||||
$orderBy = $assoc['orderBy'];
|
||||
}
|
||||
|
||||
if ($orderBy) {
|
||||
$orderBySql = $this->getOrderBySQL($orderBy, $baseTableAlias);
|
||||
}
|
||||
|
||||
$lockSql = '';
|
||||
|
||||
if ($lockMode == LockMode::PESSIMISTIC_READ) {
|
||||
$lockSql = ' ' . $this->_platform->getReadLockSql();
|
||||
} else if ($lockMode == LockMode::PESSIMISTIC_WRITE) {
|
||||
$lockSql = ' ' . $this->_platform->getWriteLockSql();
|
||||
switch ($lockMode) {
|
||||
case LockMode::PESSIMISTIC_READ:
|
||||
|
||||
$lockSql = ' ' . $this->platform->getReadLockSql();
|
||||
|
||||
break;
|
||||
|
||||
case LockMode::PESSIMISTIC_WRITE:
|
||||
|
||||
$lockSql = ' ' . $this->platform->getWriteLockSql();
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return $this->_platform->modifyLimitQuery('SELECT ' . $this->_selectColumnListSql
|
||||
. ' FROM ' . $this->quoteStrategy->getTableName($this->_class, $this->_platform) . ' ' . $baseTableAlias
|
||||
. $joinSql
|
||||
. ($conditionSql != '' ? ' WHERE ' . $conditionSql : '') . $orderBySql, $limit, $offset)
|
||||
. $lockSql;
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -417,64 +386,155 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
|
||||
*/
|
||||
public function getLockTablesSql()
|
||||
{
|
||||
$idColumns = $this->_class->getIdentifierColumnNames();
|
||||
$baseTableAlias = $this->_getSQLTableAlias($this->_class->name);
|
||||
$joinSql = '';
|
||||
$identifierColumns = $this->class->getIdentifierColumnNames();
|
||||
$baseTableAlias = $this->getSQLTableAlias($this->class->name);
|
||||
$quotedTableName = $this->quoteStrategy->getTableName($this->class, $this->platform);
|
||||
|
||||
// INNER JOIN parent tables
|
||||
$joinSql = '';
|
||||
foreach ($this->class->parentClasses as $parentClassName) {
|
||||
$conditions = array();
|
||||
$tableAlias = $this->getSQLTableAlias($parentClassName);
|
||||
$parentClass = $this->em->getClassMetadata($parentClassName);
|
||||
$joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON ';
|
||||
|
||||
foreach ($this->_class->parentClasses as $parentClassName) {
|
||||
$parentClass = $this->_em->getClassMetadata($parentClassName);
|
||||
$tableAlias = $this->_getSQLTableAlias($parentClassName);
|
||||
$joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->_platform) . ' ' . $tableAlias . ' ON ';
|
||||
$first = true;
|
||||
|
||||
foreach ($idColumns as $idColumn) {
|
||||
if ($first) $first = false; else $joinSql .= ' AND ';
|
||||
|
||||
$joinSql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
||||
foreach ($identifierColumns as $idColumn) {
|
||||
$conditions[] = $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
||||
}
|
||||
|
||||
$joinSql .= implode(' AND ', $conditions);
|
||||
}
|
||||
|
||||
return 'FROM ' .$this->quoteStrategy->getTableName($this->_class, $this->_platform) . ' ' . $baseTableAlias . $joinSql;
|
||||
return 'FROM ' . $quotedTableName . ' ' . $baseTableAlias . $joinSql;
|
||||
}
|
||||
|
||||
/* Ensure this method is never called. This persister overrides _getSelectEntitiesSQL directly. */
|
||||
protected function _getSelectColumnListSQL()
|
||||
/*
|
||||
* Ensure this method is never called. This persister overrides getSelectEntitiesSQL directly.
|
||||
*/
|
||||
protected function getSelectColumnsSQL()
|
||||
{
|
||||
throw new \BadMethodCallException("Illegal invocation of ".__METHOD__.".");
|
||||
}
|
||||
// Create the column list fragment only once
|
||||
if ($this->selectColumnListSql !== null) {
|
||||
return $this->selectColumnListSql;
|
||||
}
|
||||
|
||||
/** {@inheritdoc} */
|
||||
protected function _getInsertColumnList()
|
||||
{
|
||||
// Identifier columns must always come first in the column list of subclasses.
|
||||
$columns = $this->_class->parentClasses ? $this->_class->getIdentifierColumnNames() : array();
|
||||
$columnList = array();
|
||||
$this->rsm = new ResultSetMapping();
|
||||
$discrColumn = $this->class->discriminatorColumn['name'];
|
||||
$baseTableAlias = $this->getSQLTableAlias($this->class->name);
|
||||
$resultColumnName = $this->platform->getSQLResultCasing($discrColumn);
|
||||
|
||||
foreach ($this->_class->reflFields as $name => $field) {
|
||||
if (isset($this->_class->fieldMappings[$name]['inherited']) && ! isset($this->_class->fieldMappings[$name]['id'])
|
||||
|| isset($this->_class->associationMappings[$name]['inherited'])
|
||||
|| ($this->_class->isVersioned && $this->_class->versionField == $name)) {
|
||||
$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;
|
||||
}
|
||||
|
||||
if (isset($this->_class->associationMappings[$name])) {
|
||||
$assoc = $this->_class->associationMappings[$name];
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
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->associationMappings[$name]['inherited'])
|
||||
|| ($this->class->isVersioned && $this->class->versionField == $name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($this->class->associationMappings[$name])) {
|
||||
$assoc = $this->class->associationMappings[$name];
|
||||
if ($assoc['type'] & ClassMetadata::TO_ONE && $assoc['isOwningSide']) {
|
||||
foreach ($assoc['targetToSourceKeyColumns'] as $sourceCol) {
|
||||
$columns[] = $sourceCol;
|
||||
}
|
||||
}
|
||||
} 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'];
|
||||
} 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'];
|
||||
}
|
||||
}
|
||||
|
||||
// Add discriminator column if it is the topmost class.
|
||||
if ($this->_class->name == $this->_class->rootEntityName) {
|
||||
$columns[] = $this->_class->discriminatorColumn['name'];
|
||||
if ($this->class->name == $this->class->rootEntityName) {
|
||||
$columns[] = $this->class->discriminatorColumn['name'];
|
||||
}
|
||||
|
||||
return $columns;
|
||||
@ -485,8 +545,8 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
|
||||
*/
|
||||
protected function assignDefaultVersionValue($entity, $id)
|
||||
{
|
||||
$value = $this->fetchVersionValue($this->_getVersionedClassMetadata(), $id);
|
||||
$this->_class->setFieldValue($entity, $this->_class->versionField, $value);
|
||||
$value = $this->fetchVersionValue($this->getVersionedClassMetadata(), $id);
|
||||
$this->class->setFieldValue($entity, $this->class->versionField, $value);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -38,11 +38,12 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
protected function _getDeleteRowSQL(PersistentCollection $coll)
|
||||
protected function getDeleteRowSQL(PersistentCollection $coll)
|
||||
{
|
||||
$columns = array();
|
||||
$mapping = $coll->getMapping();
|
||||
$class = $this->_em->getClassMetadata(get_class($coll->getOwner()));
|
||||
$columns = array();
|
||||
$mapping = $coll->getMapping();
|
||||
$class = $this->em->getClassMetadata(get_class($coll->getOwner()));
|
||||
$tableName = $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform);
|
||||
|
||||
foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) {
|
||||
$columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
|
||||
@ -52,7 +53,7 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
||||
$columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
|
||||
}
|
||||
|
||||
return 'DELETE FROM ' . $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform)
|
||||
return 'DELETE FROM ' . $tableName
|
||||
. ' WHERE ' . implode(' = ? AND ', $columns) . ' = ?';
|
||||
}
|
||||
|
||||
@ -60,34 +61,34 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @override
|
||||
* @internal Order of the parameters must be the same as the order of the columns in
|
||||
* _getDeleteRowSql.
|
||||
* @internal Order of the parameters must be the same as the order of the columns in getDeleteRowSql.
|
||||
*/
|
||||
protected function _getDeleteRowSQLParameters(PersistentCollection $coll, $element)
|
||||
protected function getDeleteRowSQLParameters(PersistentCollection $coll, $element)
|
||||
{
|
||||
return $this->_collectJoinTableColumnParameters($coll, $element);
|
||||
return $this->collectJoinTableColumnParameters($coll, $element);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws \BadMethodCallException Not used for OneToManyPersister
|
||||
*/
|
||||
protected function getUpdateRowSQL(PersistentCollection $coll)
|
||||
{
|
||||
throw new \BadMethodCallException("Insert Row SQL is not used for ManyToManyPersister");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @override
|
||||
* @internal Order of the parameters must be the same as the order of the columns in getInsertRowSql.
|
||||
*/
|
||||
protected function _getUpdateRowSQL(PersistentCollection $coll)
|
||||
{}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @override
|
||||
* @internal Order of the parameters must be the same as the order of the columns in
|
||||
* _getInsertRowSql.
|
||||
*/
|
||||
protected function _getInsertRowSQL(PersistentCollection $coll)
|
||||
protected function getInsertRowSQL(PersistentCollection $coll)
|
||||
{
|
||||
$columns = array();
|
||||
$mapping = $coll->getMapping();
|
||||
$class = $this->_em->getClassMetadata(get_class($coll->getOwner()));
|
||||
$class = $this->em->getClassMetadata(get_class($coll->getOwner()));
|
||||
$joinTable = $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform);
|
||||
|
||||
foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) {
|
||||
@ -106,33 +107,32 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @override
|
||||
* @internal Order of the parameters must be the same as the order of the columns in
|
||||
* _getInsertRowSql.
|
||||
* @internal Order of the parameters must be the same as the order of the columns in getInsertRowSql.
|
||||
*/
|
||||
protected function _getInsertRowSQLParameters(PersistentCollection $coll, $element)
|
||||
protected function getInsertRowSQLParameters(PersistentCollection $coll, $element)
|
||||
{
|
||||
return $this->_collectJoinTableColumnParameters($coll, $element);
|
||||
return $this->collectJoinTableColumnParameters($coll, $element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects the parameters for inserting/deleting on the join table in the order
|
||||
* of the join table columns as specified in ManyToManyMapping#joinTableColumns.
|
||||
*
|
||||
* @param $coll
|
||||
* @param $element
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
* @param object $element
|
||||
* @return array
|
||||
*/
|
||||
private function _collectJoinTableColumnParameters(PersistentCollection $coll, $element)
|
||||
private function collectJoinTableColumnParameters(PersistentCollection $coll, $element)
|
||||
{
|
||||
$params = array();
|
||||
$mapping = $coll->getMapping();
|
||||
$isComposite = count($mapping['joinTableColumns']) > 2;
|
||||
|
||||
$identifier1 = $this->_uow->getEntityIdentifier($coll->getOwner());
|
||||
$identifier2 = $this->_uow->getEntityIdentifier($element);
|
||||
$identifier1 = $this->uow->getEntityIdentifier($coll->getOwner());
|
||||
$identifier2 = $this->uow->getEntityIdentifier($element);
|
||||
|
||||
if ($isComposite) {
|
||||
$class1 = $this->_em->getClassMetadata(get_class($coll->getOwner()));
|
||||
$class1 = $this->em->getClassMetadata(get_class($coll->getOwner()));
|
||||
$class2 = $coll->getTypeClass();
|
||||
}
|
||||
|
||||
@ -162,11 +162,11 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
protected function _getDeleteSQL(PersistentCollection $coll)
|
||||
protected function getDeleteSQL(PersistentCollection $coll)
|
||||
{
|
||||
$columns = array();
|
||||
$mapping = $coll->getMapping();
|
||||
$class = $this->_em->getClassMetadata(get_class($coll->getOwner()));
|
||||
$class = $this->em->getClassMetadata(get_class($coll->getOwner()));
|
||||
$joinTable = $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform);
|
||||
|
||||
foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) {
|
||||
@ -181,12 +181,11 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @override
|
||||
* @internal Order of the parameters must be the same as the order of the columns in
|
||||
* _getDeleteSql.
|
||||
* @internal Order of the parameters must be the same as the order of the columns in getDeleteSql.
|
||||
*/
|
||||
protected function _getDeleteSQLParameters(PersistentCollection $coll)
|
||||
protected function getDeleteSQLParameters(PersistentCollection $coll)
|
||||
{
|
||||
$identifier = $this->_uow->getEntityIdentifier($coll->getOwner());
|
||||
$identifier = $this->uow->getEntityIdentifier($coll->getOwner());
|
||||
$mapping = $coll->getMapping();
|
||||
$params = array();
|
||||
|
||||
@ -198,7 +197,7 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
||||
}
|
||||
|
||||
// Composite identifier
|
||||
$sourceClass = $this->_em->getClassMetadata(get_class($coll->getOwner()));
|
||||
$sourceClass = $this->em->getClassMetadata(get_class($coll->getOwner()));
|
||||
|
||||
foreach ($mapping['relationToSourceKeyColumns'] as $srcColumn) {
|
||||
$params[] = $identifier[$sourceClass->fieldNames[$srcColumn]];
|
||||
@ -216,11 +215,11 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
||||
$params = array();
|
||||
$mapping = $coll->getMapping();
|
||||
$association = $mapping;
|
||||
$class = $this->_em->getClassMetadata($mapping['sourceEntity']);
|
||||
$id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner());
|
||||
$class = $this->em->getClassMetadata($mapping['sourceEntity']);
|
||||
$id = $this->em->getUnitOfWork()->getEntityIdentifier($coll->getOwner());
|
||||
|
||||
if ( ! $mapping['isOwningSide']) {
|
||||
$targetEntity = $this->_em->getClassMetadata($mapping['targetEntity']);
|
||||
$targetEntity = $this->em->getClassMetadata($mapping['targetEntity']);
|
||||
$association = $targetEntity->associationMappings[$mapping['mappedBy']];
|
||||
}
|
||||
|
||||
@ -249,11 +248,11 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
||||
. $joinTargetEntitySQL
|
||||
. ' WHERE ' . implode(' AND ', $conditions);
|
||||
|
||||
return $this->_conn->fetchColumn($sql, $params);
|
||||
return $this->conn->fetchColumn($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PersistentCollection $coll
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
* @param int $offset
|
||||
* @param int $length
|
||||
* @return array
|
||||
@ -262,17 +261,17 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
||||
{
|
||||
$mapping = $coll->getMapping();
|
||||
|
||||
return $this->_em->getUnitOfWork()->getEntityPersister($mapping['targetEntity'])->getManyToManyCollection($mapping, $coll->getOwner(), $offset, $length);
|
||||
return $this->em->getUnitOfWork()->getEntityPersister($mapping['targetEntity'])->getManyToManyCollection($mapping, $coll->getOwner(), $offset, $length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PersistentCollection $coll
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
* @param object $element
|
||||
* @return boolean
|
||||
*/
|
||||
public function contains(PersistentCollection $coll, $element)
|
||||
{
|
||||
$uow = $this->_em->getUnitOfWork();
|
||||
$uow = $this->em->getUnitOfWork();
|
||||
|
||||
// Shortcut for new entities
|
||||
$entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW);
|
||||
@ -290,17 +289,17 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
||||
|
||||
$sql = 'SELECT 1 FROM ' . $quotedJoinTable . ' WHERE ' . implode(' AND ', $whereClauses);
|
||||
|
||||
return (bool) $this->_conn->fetchColumn($sql, $params);
|
||||
return (bool) $this->conn->fetchColumn($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PersistentCollection $coll
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
* @param object $element
|
||||
* @return boolean
|
||||
*/
|
||||
public function removeElement(PersistentCollection $coll, $element)
|
||||
{
|
||||
$uow = $this->_em->getUnitOfWork();
|
||||
$uow = $this->em->getUnitOfWork();
|
||||
|
||||
// shortcut for new entities
|
||||
$entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW);
|
||||
@ -319,7 +318,7 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
||||
|
||||
$sql = 'DELETE FROM ' . $quotedJoinTable . ' WHERE ' . implode(' AND ', $whereClauses);
|
||||
|
||||
return (bool) $this->_conn->executeUpdate($sql, $params);
|
||||
return (bool) $this->conn->executeUpdate($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -330,19 +329,20 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
||||
*/
|
||||
private function getJoinTableRestrictions(PersistentCollection $coll, $element, $addFilters)
|
||||
{
|
||||
$uow = $this->_em->getUnitOfWork();
|
||||
$mapping = $filterMapping = $coll->getMapping();
|
||||
$uow = $this->em->getUnitOfWork();
|
||||
$filterMapping = $coll->getMapping();
|
||||
$mapping = $filterMapping;
|
||||
|
||||
if ( ! $mapping['isOwningSide']) {
|
||||
$sourceClass = $this->_em->getClassMetadata($mapping['targetEntity']);
|
||||
$targetClass = $this->_em->getClassMetadata($mapping['sourceEntity']);
|
||||
$sourceClass = $this->em->getClassMetadata($mapping['targetEntity']);
|
||||
$targetClass = $this->em->getClassMetadata($mapping['sourceEntity']);
|
||||
$sourceId = $uow->getEntityIdentifier($element);
|
||||
$targetId = $uow->getEntityIdentifier($coll->getOwner());
|
||||
|
||||
$mapping = $sourceClass->associationMappings[$mapping['mappedBy']];
|
||||
} else {
|
||||
$sourceClass = $this->_em->getClassMetadata($mapping['sourceEntity']);
|
||||
$targetClass = $this->_em->getClassMetadata($mapping['targetEntity']);
|
||||
$sourceClass = $this->em->getClassMetadata($mapping['sourceEntity']);
|
||||
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
|
||||
$sourceId = $uow->getEntityIdentifier($coll->getOwner());
|
||||
$targetId = $uow->getEntityIdentifier($element);
|
||||
}
|
||||
@ -393,33 +393,39 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
||||
*/
|
||||
public function getFilterSql($mapping)
|
||||
{
|
||||
$targetClass = $this->_em->getClassMetadata($mapping['targetEntity']);
|
||||
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
|
||||
$rootClass = $this->em->getClassMetadata($targetClass->rootEntityName);
|
||||
$filterSql = $this->generateFilterConditionSQL($rootClass, 'te');
|
||||
|
||||
if ($mapping['isOwningSide']) {
|
||||
$joinColumns = $mapping['relationToTargetKeyColumns'];
|
||||
} else {
|
||||
$mapping = $targetClass->associationMappings[$mapping['mappedBy']];
|
||||
$joinColumns = $mapping['relationToSourceKeyColumns'];
|
||||
if ('' === $filterSql) {
|
||||
return array('', '');
|
||||
}
|
||||
|
||||
$targetClass = $this->_em->getClassMetadata($targetClass->rootEntityName);
|
||||
$conditions = array();
|
||||
$association = $mapping;
|
||||
|
||||
if ( ! $mapping['isOwningSide']) {
|
||||
$class = $this->em->getClassMetadata($mapping['targetEntity']);
|
||||
$association = $class->associationMappings[$mapping['mappedBy']];
|
||||
}
|
||||
|
||||
// A join is needed if there is filtering on the target entity
|
||||
$joinTargetEntitySQL = '';
|
||||
if ($filterSql = $this->generateFilterConditionSQL($targetClass, 'te')) {
|
||||
$joinTargetEntitySQL = ' JOIN '
|
||||
. $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' te'
|
||||
. ' ON';
|
||||
$tableName = $this->quoteStrategy->getTableName($rootClass, $this->platform);
|
||||
$joinSql = ' JOIN ' . $tableName . ' te' . ' ON';
|
||||
$joinColumns = $mapping['isOwningSide']
|
||||
? $association['joinTable']['inverseJoinColumns']
|
||||
: $association['joinTable']['joinColumns'];
|
||||
|
||||
$joinTargetEntitySQLClauses = array();
|
||||
foreach ($joinColumns as $joinTableColumn => $targetTableColumn) {
|
||||
$joinTargetEntitySQLClauses[] = ' t.' . $joinTableColumn . ' = ' . 'te.' . $targetTableColumn;
|
||||
}
|
||||
foreach ($joinColumns as $joinColumn) {
|
||||
$joinColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform);
|
||||
$refColumnName = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform);
|
||||
|
||||
$joinTargetEntitySQL .= implode(' AND ', $joinTargetEntitySQLClauses);
|
||||
$conditions[] = ' t.' . $joinColumnName . ' = ' . 'te.' . $refColumnName;
|
||||
}
|
||||
|
||||
return array($joinTargetEntitySQL, $filterSql);
|
||||
$joinSql .= implode(' AND ', $conditions);
|
||||
|
||||
return array($joinSql, $filterSql);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -434,7 +440,7 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
||||
{
|
||||
$filterClauses = array();
|
||||
|
||||
foreach ($this->_em->getFilters()->getEnabledFilters() as $filter) {
|
||||
foreach ($this->em->getFilters()->getEnabledFilters() as $filter) {
|
||||
if ($filterExpr = $filter->addFilterConstraint($targetEntity, $targetTableAlias)) {
|
||||
$filterClauses[] = '(' . $filterExpr . ')';
|
||||
}
|
||||
|
@ -36,82 +36,93 @@ class OneToManyPersister extends AbstractCollectionPersister
|
||||
* Generates the SQL UPDATE that updates a particular row's foreign
|
||||
* key to null.
|
||||
*
|
||||
* @param PersistentCollection $coll
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
* @return string
|
||||
* @override
|
||||
*/
|
||||
protected function _getDeleteRowSQL(PersistentCollection $coll)
|
||||
protected function getDeleteRowSQL(PersistentCollection $coll)
|
||||
{
|
||||
$mapping = $coll->getMapping();
|
||||
$class = $this->_em->getClassMetadata($mapping['targetEntity']);
|
||||
$mapping = $coll->getMapping();
|
||||
$class = $this->em->getClassMetadata($mapping['targetEntity']);
|
||||
$tableName = $this->quoteStrategy->getTableName($class, $this->platform);
|
||||
$idColumns = $class->getIdentifierColumnNames();
|
||||
|
||||
return 'DELETE FROM ' . $this->quoteStrategy->getTableName($class, $this->platform)
|
||||
. ' WHERE ' . implode('= ? AND ', $class->getIdentifierColumnNames()) . ' = ?';
|
||||
return 'DELETE FROM ' . $tableName
|
||||
. ' WHERE ' . implode('= ? AND ', $idColumns) . ' = ?';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getDeleteRowSQLParameters(PersistentCollection $coll, $element)
|
||||
{
|
||||
return array_values($this->uow->getEntityIdentifier($element));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws \BadMethodCallException Not used for OneToManyPersister
|
||||
*/
|
||||
protected function getInsertRowSQL(PersistentCollection $coll)
|
||||
{
|
||||
throw new \BadMethodCallException("Insert Row SQL is not used for OneToManyPersister");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws \BadMethodCallException Not used for OneToManyPersister
|
||||
*/
|
||||
protected function getInsertRowSQLParameters(PersistentCollection $coll, $element)
|
||||
{
|
||||
throw new \BadMethodCallException("Insert Row SQL is not used for OneToManyPersister");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws \BadMethodCallException Not used for OneToManyPersister
|
||||
*/
|
||||
protected function _getDeleteRowSQLParameters(PersistentCollection $coll, $element)
|
||||
protected function getUpdateRowSQL(PersistentCollection $coll)
|
||||
{
|
||||
return array_values($this->_uow->getEntityIdentifier($element));
|
||||
}
|
||||
|
||||
protected function _getInsertRowSQL(PersistentCollection $coll)
|
||||
{
|
||||
return "UPDATE xxx SET foreign_key = yyy WHERE foreign_key = zzz";
|
||||
throw new \BadMethodCallException("Update Row SQL is not used for OneToManyPersister");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the SQL parameters for the corresponding SQL statement to insert the given
|
||||
* element of the given collection into the database.
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param PersistentCollection $coll
|
||||
* @param mixed $element
|
||||
* @throws \BadMethodCallException Not used for OneToManyPersister
|
||||
*/
|
||||
protected function _getInsertRowSQLParameters(PersistentCollection $coll, $element)
|
||||
{}
|
||||
|
||||
/* Not used for OneToManyPersister */
|
||||
protected function _getUpdateRowSQL(PersistentCollection $coll)
|
||||
protected function getDeleteSQL(PersistentCollection $coll)
|
||||
{
|
||||
return;
|
||||
throw new \BadMethodCallException("Update Row SQL is not used for OneToManyPersister");
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the SQL UPDATE that updates all the foreign keys to null.
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param PersistentCollection $coll
|
||||
* @throws \BadMethodCallException Not used for OneToManyPersister
|
||||
*/
|
||||
protected function _getDeleteSQL(PersistentCollection $coll)
|
||||
protected function getDeleteSQLParameters(PersistentCollection $coll)
|
||||
{
|
||||
|
||||
throw new \BadMethodCallException("Update Row SQL is not used for OneToManyPersister");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the SQL parameters for the corresponding SQL statement to delete
|
||||
* the given collection.
|
||||
*
|
||||
* @param PersistentCollection $coll
|
||||
*/
|
||||
protected function _getDeleteSQLParameters(PersistentCollection $coll)
|
||||
{}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function count(PersistentCollection $coll)
|
||||
{
|
||||
$mapping = $coll->getMapping();
|
||||
$targetClass = $this->_em->getClassMetadata($mapping['targetEntity']);
|
||||
$sourceClass = $this->_em->getClassMetadata($mapping['sourceEntity']);
|
||||
$id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner());
|
||||
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
|
||||
$sourceClass = $this->em->getClassMetadata($mapping['sourceEntity']);
|
||||
$id = $this->em->getUnitOfWork()->getEntityIdentifier($coll->getOwner());
|
||||
|
||||
$whereClauses = array();
|
||||
$params = array();
|
||||
|
||||
foreach ($targetClass->associationMappings[$mapping['mappedBy']]['joinColumns'] as $joinColumn) {
|
||||
$joinColumns = $targetClass->associationMappings[$mapping['mappedBy']]['joinColumns'];
|
||||
foreach ($joinColumns as $joinColumn) {
|
||||
$whereClauses[] = $joinColumn['name'] . ' = ?';
|
||||
|
||||
$params[] = ($targetClass->containsForeignIdentifier)
|
||||
@ -119,8 +130,8 @@ class OneToManyPersister extends AbstractCollectionPersister
|
||||
: $id[$sourceClass->fieldNames[$joinColumn['referencedColumnName']]];
|
||||
}
|
||||
|
||||
$filterTargetClass = $this->_em->getClassMetadata($targetClass->rootEntityName);
|
||||
foreach ($this->_em->getFilters()->getEnabledFilters() as $filter) {
|
||||
$filterTargetClass = $this->em->getClassMetadata($targetClass->rootEntityName);
|
||||
foreach ($this->em->getFilters()->getEnabledFilters() as $filter) {
|
||||
if ($filterExpr = $filter->addFilterConstraint($filterTargetClass, 't')) {
|
||||
$whereClauses[] = '(' . $filterExpr . ')';
|
||||
}
|
||||
@ -130,11 +141,11 @@ class OneToManyPersister extends AbstractCollectionPersister
|
||||
. ' FROM ' . $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' t'
|
||||
. ' WHERE ' . implode(' AND ', $whereClauses);
|
||||
|
||||
return $this->_conn->fetchColumn($sql, $params);
|
||||
return $this->conn->fetchColumn($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PersistentCollection $coll
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
* @param int $offset
|
||||
* @param int $length
|
||||
* @return \Doctrine\Common\Collections\ArrayCollection
|
||||
@ -142,21 +153,21 @@ class OneToManyPersister extends AbstractCollectionPersister
|
||||
public function slice(PersistentCollection $coll, $offset, $length = null)
|
||||
{
|
||||
$mapping = $coll->getMapping();
|
||||
$uow = $this->_em->getUnitOfWork();
|
||||
$uow = $this->em->getUnitOfWork();
|
||||
$persister = $uow->getEntityPersister($mapping['targetEntity']);
|
||||
|
||||
return $persister->getOneToManyCollection($mapping, $coll->getOwner(), $offset, $length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PersistentCollection $coll
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
* @param object $element
|
||||
* @return boolean
|
||||
*/
|
||||
public function contains(PersistentCollection $coll, $element)
|
||||
{
|
||||
$mapping = $coll->getMapping();
|
||||
$uow = $this->_em->getUnitOfWork();
|
||||
$uow = $this->em->getUnitOfWork();
|
||||
|
||||
// shortcut for new entities
|
||||
$entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW);
|
||||
@ -181,13 +192,13 @@ class OneToManyPersister extends AbstractCollectionPersister
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PersistentCollection $coll
|
||||
* @param \Doctrine\ORM\PersistentCollection $coll
|
||||
* @param object $element
|
||||
* @return boolean
|
||||
*/
|
||||
public function removeElement(PersistentCollection $coll, $element)
|
||||
{
|
||||
$uow = $this->_em->getUnitOfWork();
|
||||
$uow = $this->em->getUnitOfWork();
|
||||
|
||||
// shortcut for new entities
|
||||
$entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW);
|
||||
@ -203,10 +214,10 @@ class OneToManyPersister extends AbstractCollectionPersister
|
||||
}
|
||||
|
||||
$mapping = $coll->getMapping();
|
||||
$class = $this->_em->getClassMetadata($mapping['targetEntity']);
|
||||
$class = $this->em->getClassMetadata($mapping['targetEntity']);
|
||||
$sql = 'DELETE FROM ' . $this->quoteStrategy->getTableName($class, $this->platform)
|
||||
. ' WHERE ' . implode('= ? AND ', $class->getIdentifierColumnNames()) . ' = ?';
|
||||
|
||||
return (bool) $this->_conn->executeUpdate($sql, $this->_getDeleteRowSQLParameters($coll, $element));
|
||||
return (bool) $this->conn->executeUpdate($sql, $this->getDeleteRowSQLParameters($coll, $element));
|
||||
}
|
||||
}
|
||||
|
@ -34,128 +34,147 @@ use Doctrine\Common\Collections\Criteria;
|
||||
*/
|
||||
class SingleTablePersister extends AbstractEntityInheritancePersister
|
||||
{
|
||||
/** {@inheritdoc} */
|
||||
protected function _getDiscriminatorColumnTableName()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getDiscriminatorColumnTableName()
|
||||
{
|
||||
return $this->_class->getTableName();
|
||||
return $this->class->getTableName();
|
||||
}
|
||||
|
||||
/** {@inheritdoc} */
|
||||
protected function _getSelectColumnListSQL()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getSelectColumnsSQL()
|
||||
{
|
||||
if ($this->_selectColumnListSql !== null) {
|
||||
return $this->_selectColumnListSql;
|
||||
if ($this->selectColumnListSql !== null) {
|
||||
return $this->selectColumnListSql;
|
||||
}
|
||||
|
||||
$columnList = parent::_getSelectColumnListSQL();
|
||||
$columnList[] = parent::getSelectColumnsSQL();
|
||||
|
||||
$rootClass = $this->_em->getClassMetadata($this->_class->rootEntityName);
|
||||
$tableAlias = $this->_getSQLTableAlias($rootClass->name);
|
||||
$rootClass = $this->em->getClassMetadata($this->class->rootEntityName);
|
||||
$tableAlias = $this->getSQLTableAlias($rootClass->name);
|
||||
|
||||
// Append discriminator column
|
||||
$discrColumn = $this->_class->discriminatorColumn['name'];
|
||||
$columnList .= ', ' . $tableAlias . '.' . $discrColumn;
|
||||
$discrColumn = $this->class->discriminatorColumn['name'];
|
||||
$columnList[] = $tableAlias . '.' . $discrColumn;
|
||||
|
||||
$resultColumnName = $this->_platform->getSQLResultCasing($discrColumn);
|
||||
$resultColumnName = $this->platform->getSQLResultCasing($discrColumn);
|
||||
|
||||
$this->_rsm->setDiscriminatorColumn('r', $resultColumnName);
|
||||
$this->_rsm->addMetaResult('r', $resultColumnName, $discrColumn);
|
||||
$this->rsm->setDiscriminatorColumn('r', $resultColumnName);
|
||||
$this->rsm->addMetaResult('r', $resultColumnName, $discrColumn);
|
||||
|
||||
// Append subclass columns
|
||||
foreach ($this->_class->subClasses as $subClassName) {
|
||||
$subClass = $this->_em->getClassMetadata($subClassName);
|
||||
foreach ($this->class->subClasses as $subClassName) {
|
||||
$subClass = $this->em->getClassMetadata($subClassName);
|
||||
|
||||
// Regular columns
|
||||
foreach ($subClass->fieldMappings as $fieldName => $mapping) {
|
||||
if ( ! isset($mapping['inherited'])) {
|
||||
$columnList .= ', ' . $this->_getSelectColumnSQL($fieldName, $subClass);
|
||||
if (isset($mapping['inherited'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$columnList[] = $this->getSelectColumnSQL($fieldName, $subClass);
|
||||
}
|
||||
|
||||
// Foreign key columns
|
||||
foreach ($subClass->associationMappings as $assoc) {
|
||||
if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE && ! isset($assoc['inherited'])) {
|
||||
foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) {
|
||||
if ($columnList != '') $columnList .= ', ';
|
||||
if ( ! $assoc['isOwningSide']
|
||||
|| ! ($assoc['type'] & ClassMetadata::TO_ONE)
|
||||
|| isset($assoc['inherited'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$columnList .= $this->getSelectJoinColumnSQL(
|
||||
$tableAlias,
|
||||
$srcColumn,
|
||||
isset($assoc['inherited']) ? $assoc['inherited'] : $this->_class->name
|
||||
);
|
||||
}
|
||||
foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) {
|
||||
$className = isset($assoc['inherited']) ? $assoc['inherited'] : $this->class->name;
|
||||
$columnList[] = $this->getSelectJoinColumnSQL($tableAlias, $srcColumn, $className);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->_selectColumnListSql = $columnList;
|
||||
return $this->_selectColumnListSql;
|
||||
$this->selectColumnListSql = implode(', ', $columnList);
|
||||
|
||||
return $this->selectColumnListSql;
|
||||
}
|
||||
|
||||
/** {@inheritdoc} */
|
||||
protected function _getInsertColumnList()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getInsertColumnList()
|
||||
{
|
||||
$columns = parent::_getInsertColumnList();
|
||||
$columns = parent::getInsertColumnList();
|
||||
|
||||
// Add discriminator column to the INSERT SQL
|
||||
$columns[] = $this->_class->discriminatorColumn['name'];
|
||||
$columns[] = $this->class->discriminatorColumn['name'];
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
/** {@inheritdoc} */
|
||||
protected function _getSQLTableAlias($className, $assocName = '')
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getSQLTableAlias($className, $assocName = '')
|
||||
{
|
||||
return parent::_getSQLTableAlias($this->_class->rootEntityName, $assocName);
|
||||
return parent::getSQLTableAlias($this->class->rootEntityName, $assocName);
|
||||
}
|
||||
|
||||
/** {@inheritdoc} */
|
||||
protected function _getSelectConditionSQL(array $criteria, $assoc = null)
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getSelectConditionSQL(array $criteria, $assoc = null)
|
||||
{
|
||||
$conditionSql = parent::_getSelectConditionSQL($criteria, $assoc);
|
||||
$conditionSql = parent::getSelectConditionSQL($criteria, $assoc);
|
||||
|
||||
if ($conditionSql) {
|
||||
$conditionSql .= ' AND ';
|
||||
}
|
||||
|
||||
return $conditionSql . $this->_getSelectConditionDiscriminatorValueSQL();
|
||||
return $conditionSql . $this->getSelectConditionDiscriminatorValueSQL();
|
||||
}
|
||||
|
||||
/** {@inheritdoc} */
|
||||
protected function _getSelectConditionCriteriaSQL(Criteria $criteria)
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getSelectConditionCriteriaSQL(Criteria $criteria)
|
||||
{
|
||||
$conditionSql = parent::_getSelectConditionCriteriaSQL($criteria);
|
||||
$conditionSql = parent::getSelectConditionCriteriaSQL($criteria);
|
||||
|
||||
if ($conditionSql) {
|
||||
$conditionSql .= ' AND ';
|
||||
}
|
||||
|
||||
return $conditionSql . $this->_getSelectConditionDiscriminatorValueSQL();
|
||||
return $conditionSql . $this->getSelectConditionDiscriminatorValueSQL();
|
||||
}
|
||||
|
||||
protected function _getSelectConditionDiscriminatorValueSQL()
|
||||
protected function getSelectConditionDiscriminatorValueSQL()
|
||||
{
|
||||
$values = array();
|
||||
|
||||
if ($this->_class->discriminatorValue !== null) { // discriminators can be 0
|
||||
$values[] = $this->_conn->quote($this->_class->discriminatorValue);
|
||||
if ($this->class->discriminatorValue !== null) { // discriminators can be 0
|
||||
$values[] = $this->conn->quote($this->class->discriminatorValue);
|
||||
}
|
||||
|
||||
$discrValues = array_flip($this->_class->discriminatorMap);
|
||||
$discrValues = array_flip($this->class->discriminatorMap);
|
||||
|
||||
foreach ($this->_class->subClasses as $subclassName) {
|
||||
$values[] = $this->_conn->quote($discrValues[$subclassName]);
|
||||
foreach ($this->class->subClasses as $subclassName) {
|
||||
$values[] = $this->conn->quote($discrValues[$subclassName]);
|
||||
}
|
||||
|
||||
return $this->_getSQLTableAlias($this->_class->name) . '.' . $this->_class->discriminatorColumn['name']
|
||||
. ' IN (' . implode(', ', $values) . ')';
|
||||
$values = implode(', ', $values);
|
||||
$discColumn = $this->class->discriminatorColumn['name'];
|
||||
$tableAlias = $this->getSQLTableAlias($this->class->name);
|
||||
|
||||
return $tableAlias . '.' . $discColumn . ' IN (' . $values . ')';
|
||||
}
|
||||
|
||||
/** {@inheritdoc} */
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function generateFilterConditionSQL(ClassMetadata $targetEntity, $targetTableAlias)
|
||||
{
|
||||
// Ensure that the filters are applied to the root entity of the inheritance tree
|
||||
$targetEntity = $this->_em->getClassMetadata($targetEntity->rootEntityName);
|
||||
$targetEntity = $this->em->getClassMetadata($targetEntity->rootEntityName);
|
||||
// we dont care about the $targetTableAlias, in a STI there is only one table.
|
||||
|
||||
return parent::generateFilterConditionSQL($targetEntity, $targetTableAlias);
|
||||
|
@ -7,12 +7,12 @@ namespace Doctrine\Tests\Mocks;
|
||||
*/
|
||||
class EntityPersisterMock extends \Doctrine\ORM\Persisters\BasicEntityPersister
|
||||
{
|
||||
private $_inserts = array();
|
||||
private $_updates = array();
|
||||
private $_deletes = array();
|
||||
private $_identityColumnValueCounter = 0;
|
||||
private $_mockIdGeneratorType;
|
||||
private $_postInsertIds = array();
|
||||
private $inserts = array();
|
||||
private $updates = array();
|
||||
private $deletes = array();
|
||||
private $identityColumnValueCounter = 0;
|
||||
private $mockIdGeneratorType;
|
||||
private $postInsertIds = array();
|
||||
private $existsCalled = false;
|
||||
|
||||
/**
|
||||
@ -22,11 +22,11 @@ class EntityPersisterMock extends \Doctrine\ORM\Persisters\BasicEntityPersister
|
||||
*/
|
||||
public function insert($entity)
|
||||
{
|
||||
$this->_inserts[] = $entity;
|
||||
if ( ! is_null($this->_mockIdGeneratorType) && $this->_mockIdGeneratorType == \Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_IDENTITY
|
||||
|| $this->_class->isIdGeneratorIdentity()) {
|
||||
$id = $this->_identityColumnValueCounter++;
|
||||
$this->_postInsertIds[$id] = $entity;
|
||||
$this->inserts[] = $entity;
|
||||
if ( ! is_null($this->mockIdGeneratorType) && $this->mockIdGeneratorType == \Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_IDENTITY
|
||||
|| $this->class->isIdGeneratorIdentity()) {
|
||||
$id = $this->identityColumnValueCounter++;
|
||||
$this->postInsertIds[$id] = $entity;
|
||||
return $id;
|
||||
}
|
||||
return null;
|
||||
@ -34,11 +34,11 @@ class EntityPersisterMock extends \Doctrine\ORM\Persisters\BasicEntityPersister
|
||||
|
||||
public function addInsert($entity)
|
||||
{
|
||||
$this->_inserts[] = $entity;
|
||||
if ( ! is_null($this->_mockIdGeneratorType) && $this->_mockIdGeneratorType == \Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_IDENTITY
|
||||
|| $this->_class->isIdGeneratorIdentity()) {
|
||||
$id = $this->_identityColumnValueCounter++;
|
||||
$this->_postInsertIds[$id] = $entity;
|
||||
$this->inserts[] = $entity;
|
||||
if ( ! is_null($this->mockIdGeneratorType) && $this->mockIdGeneratorType == \Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_IDENTITY
|
||||
|| $this->class->isIdGeneratorIdentity()) {
|
||||
$id = $this->identityColumnValueCounter++;
|
||||
$this->postInsertIds[$id] = $entity;
|
||||
return $id;
|
||||
}
|
||||
return null;
|
||||
@ -46,17 +46,17 @@ class EntityPersisterMock extends \Doctrine\ORM\Persisters\BasicEntityPersister
|
||||
|
||||
public function executeInserts()
|
||||
{
|
||||
return $this->_postInsertIds;
|
||||
return $this->postInsertIds;
|
||||
}
|
||||
|
||||
public function setMockIdGeneratorType($genType)
|
||||
{
|
||||
$this->_mockIdGeneratorType = $genType;
|
||||
$this->mockIdGeneratorType = $genType;
|
||||
}
|
||||
|
||||
public function update($entity)
|
||||
{
|
||||
$this->_updates[] = $entity;
|
||||
$this->updates[] = $entity;
|
||||
}
|
||||
|
||||
public function exists($entity, array $extraConditions = array())
|
||||
@ -66,31 +66,31 @@ class EntityPersisterMock extends \Doctrine\ORM\Persisters\BasicEntityPersister
|
||||
|
||||
public function delete($entity)
|
||||
{
|
||||
$this->_deletes[] = $entity;
|
||||
$this->deletes[] = $entity;
|
||||
}
|
||||
|
||||
public function getInserts()
|
||||
{
|
||||
return $this->_inserts;
|
||||
return $this->inserts;
|
||||
}
|
||||
|
||||
public function getUpdates()
|
||||
{
|
||||
return $this->_updates;
|
||||
return $this->updates;
|
||||
}
|
||||
|
||||
public function getDeletes()
|
||||
{
|
||||
return $this->_deletes;
|
||||
return $this->deletes;
|
||||
}
|
||||
|
||||
public function reset()
|
||||
{
|
||||
$this->existsCalled = false;
|
||||
$this->_identityColumnValueCounter = 0;
|
||||
$this->_inserts = array();
|
||||
$this->_updates = array();
|
||||
$this->_deletes = array();
|
||||
$this->identityColumnValueCounter = 0;
|
||||
$this->inserts = array();
|
||||
$this->updates = array();
|
||||
$this->deletes = array();
|
||||
}
|
||||
|
||||
public function isExistsCalled()
|
||||
|
@ -39,7 +39,7 @@ class BasicEntityPersisterTypeValueSqlTest extends \Doctrine\Tests\OrmTestCase
|
||||
|
||||
public function testGetInsertSQLUsesTypeValuesSQL()
|
||||
{
|
||||
$method = new \ReflectionMethod($this->_persister, '_getInsertSQL');
|
||||
$method = new \ReflectionMethod($this->_persister, 'getInsertSQL');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$sql = $method->invoke($this->_persister);
|
||||
@ -70,7 +70,7 @@ class BasicEntityPersisterTypeValueSqlTest extends \Doctrine\Tests\OrmTestCase
|
||||
|
||||
public function testGetSelectConditionSQLUsesTypeValuesSQL()
|
||||
{
|
||||
$method = new \ReflectionMethod($this->_persister, '_getSelectConditionSQL');
|
||||
$method = new \ReflectionMethod($this->_persister, 'getSelectConditionSQL');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$sql = $method->invoke($this->_persister, array('customInteger' => 1, 'child' => 1));
|
||||
@ -84,7 +84,7 @@ class BasicEntityPersisterTypeValueSqlTest extends \Doctrine\Tests\OrmTestCase
|
||||
public function testStripNonAlphanumericCharactersFromSelectColumnListSQL()
|
||||
{
|
||||
$persister = new BasicEntityPersister($this->_em, $this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\SimpleEntity'));
|
||||
$method = new \ReflectionMethod($persister, '_getSelectColumnListSQL');
|
||||
$method = new \ReflectionMethod($persister, 'getSelectColumnsSQL');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$this->assertEquals('t0."simple-entity-id" AS simpleentityid1, t0."simple-entity-value" AS simpleentityvalue2', $method->invoke($persister));
|
||||
|
Loading…
x
Reference in New Issue
Block a user