welcome back to dyb-tech
This commit is contained in:
+81
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Persisters\Entity;
|
||||
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Base class for entity persisters that implement a certain inheritance mapping strategy.
|
||||
* All these persisters are assumed to use a discriminator column to discriminate entity
|
||||
* types in the hierarchy.
|
||||
*/
|
||||
abstract class AbstractEntityInheritancePersister extends BasicEntityPersister
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function prepareInsertData($entity)
|
||||
{
|
||||
$data = parent::prepareInsertData($entity);
|
||||
|
||||
// Populate the discriminator column
|
||||
$discColumn = $this->class->getDiscriminatorColumn();
|
||||
$this->columnTypes[$discColumn['name']] = $discColumn['type'];
|
||||
$data[$this->getDiscriminatorColumnTableName()][$discColumn['name']] = $this->class->discriminatorValue;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the table that contains the discriminator column.
|
||||
*
|
||||
* @return string The table name.
|
||||
*/
|
||||
abstract protected function getDiscriminatorColumnTableName();
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r')
|
||||
{
|
||||
$tableAlias = $alias === 'r' ? '' : $alias;
|
||||
$fieldMapping = $class->fieldMappings[$field];
|
||||
$columnAlias = $this->getSQLColumnAlias($fieldMapping['columnName']);
|
||||
$sql = sprintf(
|
||||
'%s.%s',
|
||||
$this->getSQLTableAlias($class->name, $tableAlias),
|
||||
$this->quoteStrategy->getColumnName($field, $class, $this->platform)
|
||||
);
|
||||
|
||||
$this->currentPersisterContext->rsm->addFieldResult($alias, $columnAlias, $field, $class->name);
|
||||
|
||||
if (isset($fieldMapping['requireSQLConversion'])) {
|
||||
$type = Type::getType($fieldMapping['type']);
|
||||
$sql = $type->convertToPHPValueSQL($sql, $this->platform);
|
||||
}
|
||||
|
||||
return $sql . ' AS ' . $columnAlias;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $tableAlias
|
||||
* @param string $joinColumnName
|
||||
* @param string $quotedColumnName
|
||||
* @param string $type
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getSelectJoinColumnSQL($tableAlias, $joinColumnName, $quotedColumnName, $type)
|
||||
{
|
||||
$columnAlias = $this->getSQLColumnAlias($joinColumnName);
|
||||
|
||||
$this->currentPersisterContext->rsm->addMetaResult('r', $columnAlias, $joinColumnName, false, $type);
|
||||
|
||||
return $tableAlias . '.' . $quotedColumnName . ' AS ' . $columnAlias;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Persisters\Entity;
|
||||
|
||||
use Doctrine\ORM\Query\ResultSetMapping;
|
||||
use Doctrine\Persistence\Mapping\ClassMetadata;
|
||||
|
||||
/**
|
||||
* A swappable persister context to use as a container for the current
|
||||
* generated query/resultSetMapping/type binding information.
|
||||
*
|
||||
* This class is a utility class to be used only by the persister API
|
||||
*
|
||||
* This object is highly mutable due to performance reasons. Same reasoning
|
||||
* behind its properties being public.
|
||||
*/
|
||||
class CachedPersisterContext
|
||||
{
|
||||
/**
|
||||
* Metadata object that describes the mapping of the mapped entity class.
|
||||
*
|
||||
* @var \Doctrine\ORM\Mapping\ClassMetadata
|
||||
*/
|
||||
public $class;
|
||||
|
||||
/**
|
||||
* ResultSetMapping that is used for all queries. Is generated lazily once per request.
|
||||
*
|
||||
* @var ResultSetMapping
|
||||
*/
|
||||
public $rsm;
|
||||
|
||||
/**
|
||||
* The SELECT column list SQL fragment used for querying entities by this persister.
|
||||
* This SQL fragment is only generated once per request, if at all.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
public $selectColumnListSql;
|
||||
|
||||
/**
|
||||
* The JOIN SQL fragment used to eagerly load all many-to-one and one-to-one
|
||||
* associations configured as FETCH_EAGER, as well as all inverse one-to-one associations.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $selectJoinSql;
|
||||
|
||||
/**
|
||||
* Counter for creating unique SQL table and column aliases.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $sqlAliasCounter = 0;
|
||||
|
||||
/**
|
||||
* Map from class names (FQCN) to the corresponding generated SQL table aliases.
|
||||
*
|
||||
* @var array<class-string, string>
|
||||
*/
|
||||
public $sqlTableAliases = [];
|
||||
|
||||
/**
|
||||
* Whether this persistent context is considering limit operations applied to the selection queries
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $handlesLimits;
|
||||
|
||||
/** @param bool $handlesLimits */
|
||||
public function __construct(
|
||||
ClassMetadata $class,
|
||||
ResultSetMapping $rsm,
|
||||
$handlesLimits
|
||||
) {
|
||||
$this->class = $class;
|
||||
$this->rsm = $rsm;
|
||||
$this->handlesLimits = (bool) $handlesLimits;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Persisters\Entity;
|
||||
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
use Doctrine\DBAL\LockMode;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use Doctrine\ORM\Mapping\MappingException;
|
||||
use Doctrine\ORM\PersistentCollection;
|
||||
use Doctrine\ORM\Query\ResultSetMapping;
|
||||
|
||||
/**
|
||||
* Entity persister interface
|
||||
* Define the behavior that should be implemented by all entity persisters.
|
||||
*
|
||||
* @psalm-import-type AssociationMapping from ClassMetadata
|
||||
*/
|
||||
interface EntityPersister
|
||||
{
|
||||
/** @return ClassMetadata */
|
||||
public function getClassMetadata();
|
||||
|
||||
/**
|
||||
* Gets the ResultSetMapping used for hydration.
|
||||
*
|
||||
* @return ResultSetMapping
|
||||
*/
|
||||
public function getResultSetMapping();
|
||||
|
||||
/**
|
||||
* Get all queued inserts.
|
||||
*
|
||||
* @return object[]
|
||||
*/
|
||||
public function getInserts();
|
||||
|
||||
/**
|
||||
* Gets the INSERT SQL used by the persister to persist a new entity.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @TODO It should not be here.
|
||||
* But its necessary since JoinedSubclassPersister#executeInserts invoke the root persister.
|
||||
*/
|
||||
public function getInsertSQL();
|
||||
|
||||
/**
|
||||
* Gets the SELECT SQL to select one or more entities by a set of field criteria.
|
||||
*
|
||||
* @param mixed[]|Criteria $criteria
|
||||
* @param int|null $lockMode
|
||||
* @param int|null $limit
|
||||
* @param int|null $offset
|
||||
* @param mixed[]|null $orderBy
|
||||
* @psalm-param AssociationMapping|null $assoc
|
||||
* @psalm-param LockMode::*|null $lockMode
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSelectSQL($criteria, $assoc = null, $lockMode = null, $limit = null, $offset = null, ?array $orderBy = null);
|
||||
|
||||
/**
|
||||
* Get the COUNT SQL to count entities (optionally based on a criteria)
|
||||
*
|
||||
* @param mixed[]|Criteria $criteria
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCountSQL($criteria = []);
|
||||
|
||||
/**
|
||||
* Expands the parameters from the given criteria and use the correct binding types if found.
|
||||
*
|
||||
* @param string[] $criteria
|
||||
*
|
||||
* @psalm-return array{list<mixed>, list<int|string|null>}
|
||||
*/
|
||||
public function expandParameters($criteria);
|
||||
|
||||
/**
|
||||
* Expands Criteria Parameters by walking the expressions and grabbing all parameters and types from it.
|
||||
*
|
||||
* @psalm-return array{list<mixed>, list<int|string|null>}
|
||||
*/
|
||||
public function expandCriteriaParameters(Criteria $criteria);
|
||||
|
||||
/**
|
||||
* Gets the SQL WHERE condition for matching a field with a given value.
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param AssociationMapping|null $assoc
|
||||
* @param string|null $comparison
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSelectConditionStatementSQL($field, $value, $assoc = null, $comparison = null);
|
||||
|
||||
/**
|
||||
* Adds an entity to the queued insertions.
|
||||
* The entity remains queued until {@link executeInserts} is invoked.
|
||||
*
|
||||
* @param object $entity The entity to queue for insertion.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addInsert($entity);
|
||||
|
||||
/**
|
||||
* Executes all queued entity insertions.
|
||||
*
|
||||
* If no inserts are queued, invoking this method is a NOOP.
|
||||
*
|
||||
* @psalm-return void|list<array{
|
||||
* generatedId: int,
|
||||
* entity: object
|
||||
* }> Returning an array of generated post-insert IDs is deprecated, implementations
|
||||
* should call UnitOfWork::assignPostInsertId() and return void.
|
||||
*/
|
||||
public function executeInserts();
|
||||
|
||||
/**
|
||||
* Updates a managed entity. The entity is updated according to its current changeset
|
||||
* in the running UnitOfWork. If there is no changeset, nothing is updated.
|
||||
*
|
||||
* @param object $entity The entity to update.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function update($entity);
|
||||
|
||||
/**
|
||||
* Deletes a managed entity.
|
||||
*
|
||||
* The entity to delete must be managed and have a persistent identifier.
|
||||
* The deletion happens instantaneously.
|
||||
*
|
||||
* Subclasses may override this method to customize the semantics of entity deletion.
|
||||
*
|
||||
* @param object $entity The entity to delete.
|
||||
*
|
||||
* @return bool TRUE if the entity got deleted in the database, FALSE otherwise.
|
||||
*/
|
||||
public function delete($entity);
|
||||
|
||||
/**
|
||||
* Count entities (optionally filtered by a criteria)
|
||||
*
|
||||
* @param mixed[]|Criteria $criteria
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function count($criteria = []);
|
||||
|
||||
/**
|
||||
* Gets the name of the table that owns the column the given field is mapped to.
|
||||
*
|
||||
* The default implementation in BasicEntityPersister always returns the name
|
||||
* of the table the entity type of this persister is mapped to, since an entity
|
||||
* is always persisted to a single table with a BasicEntityPersister.
|
||||
*
|
||||
* @param string $fieldName The field name.
|
||||
*
|
||||
* @return string The table name.
|
||||
*/
|
||||
public function getOwningTable($fieldName);
|
||||
|
||||
/**
|
||||
* Loads an entity by a list of field criteria.
|
||||
*
|
||||
* @param mixed[] $criteria The criteria by which to load the entity.
|
||||
* @param object|null $entity The entity to load the data into. If not specified,
|
||||
* a new entity is created.
|
||||
* @param AssociationMapping|null $assoc The association that connects the entity
|
||||
* to load to another entity, if any.
|
||||
* @param mixed[] $hints Hints for entity creation.
|
||||
* @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants
|
||||
* or NULL if no specific lock mode should be used
|
||||
* for loading the entity.
|
||||
* @param int|null $limit Limit number of results.
|
||||
* @param string[]|null $orderBy Criteria to order by.
|
||||
* @psalm-param array<string, mixed> $criteria
|
||||
* @psalm-param array<string, mixed> $hints
|
||||
* @psalm-param LockMode::*|null $lockMode
|
||||
* @psalm-param array<string, string>|null $orderBy
|
||||
*
|
||||
* @return object|null The loaded and managed entity instance or NULL if the entity can not be found.
|
||||
*
|
||||
* @todo Check identity map? loadById method? Try to guess whether $criteria is the id?
|
||||
*/
|
||||
public function load(
|
||||
array $criteria,
|
||||
$entity = null,
|
||||
$assoc = null,
|
||||
array $hints = [],
|
||||
$lockMode = null,
|
||||
$limit = null,
|
||||
?array $orderBy = null
|
||||
);
|
||||
|
||||
/**
|
||||
* Loads an entity by identifier.
|
||||
*
|
||||
* @param object|null $entity The entity to load the data into. If not specified, a new entity is created.
|
||||
* @psalm-param array<string, mixed> $identifier The entity identifier.
|
||||
*
|
||||
* @return object|null The loaded and managed entity instance or NULL if the entity can not be found.
|
||||
*
|
||||
* @todo Check parameters
|
||||
*/
|
||||
public function loadById(array $identifier, $entity = null);
|
||||
|
||||
/**
|
||||
* Loads an entity of this persister's mapped class as part of a single-valued
|
||||
* association from another entity.
|
||||
*
|
||||
* @param object $sourceEntity The entity that owns the association (not necessarily the "owning side").
|
||||
* @psalm-param array<string, mixed> $identifier The identifier of the entity to load. Must be provided if
|
||||
* the association to load represents the owning side, otherwise
|
||||
* the identifier is derived from the $sourceEntity.
|
||||
* @psalm-param AssociationMapping $assoc The association to load.
|
||||
*
|
||||
* @return object The loaded and managed entity instance or NULL if the entity can not be found.
|
||||
*
|
||||
* @throws MappingException
|
||||
*/
|
||||
public function loadOneToOneEntity(array $assoc, $sourceEntity, array $identifier = []);
|
||||
|
||||
/**
|
||||
* Refreshes a managed entity.
|
||||
*
|
||||
* @param object $entity The entity to refresh.
|
||||
* @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants
|
||||
* or NULL if no specific lock mode should be used
|
||||
* for refreshing the managed entity.
|
||||
* @psalm-param array<string, mixed> $id The identifier of the entity as an
|
||||
* associative array from column or
|
||||
* field names to values.
|
||||
* @psalm-param LockMode::*|null $lockMode
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function refresh(array $id, $entity, $lockMode = null);
|
||||
|
||||
/**
|
||||
* Loads Entities matching the given Criteria object.
|
||||
*
|
||||
* @return mixed[]
|
||||
*/
|
||||
public function loadCriteria(Criteria $criteria);
|
||||
|
||||
/**
|
||||
* Loads a list of entities by a list of field criteria.
|
||||
*
|
||||
* @param int|null $limit
|
||||
* @param int|null $offset
|
||||
* @psalm-param array<string, string>|null $orderBy
|
||||
* @psalm-param array<string, mixed> $criteria
|
||||
*/
|
||||
public function loadAll(array $criteria = [], ?array $orderBy = null, $limit = null, $offset = null);
|
||||
|
||||
/**
|
||||
* Gets (sliced or full) elements of the given collection.
|
||||
*
|
||||
* @param object $sourceEntity
|
||||
* @param int|null $offset
|
||||
* @param int|null $limit
|
||||
* @psalm-param AssociationMapping $assoc
|
||||
*
|
||||
* @return mixed[]
|
||||
*/
|
||||
public function getManyToManyCollection(array $assoc, $sourceEntity, $offset = null, $limit = null);
|
||||
|
||||
/**
|
||||
* Loads a collection of entities of a many-to-many association.
|
||||
*
|
||||
* @param object $sourceEntity The entity that owns the collection.
|
||||
* @param PersistentCollection $collection The collection to fill.
|
||||
* @psalm-param AssociationMapping $assoc The association mapping of the association being loaded.
|
||||
*
|
||||
* @return mixed[]
|
||||
*/
|
||||
public function loadManyToManyCollection(array $assoc, $sourceEntity, PersistentCollection $collection);
|
||||
|
||||
/**
|
||||
* Loads a collection of entities in a one-to-many association.
|
||||
*
|
||||
* @param object $sourceEntity
|
||||
* @param PersistentCollection $collection The collection to load/fill.
|
||||
* @psalm-param AssociationMapping $assoc
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function loadOneToManyCollection(array $assoc, $sourceEntity, PersistentCollection $collection);
|
||||
|
||||
/**
|
||||
* Locks all rows of this entity matching the given criteria with the specified pessimistic lock mode.
|
||||
*
|
||||
* @param int $lockMode One of the Doctrine\DBAL\LockMode::* constants.
|
||||
* @psalm-param array<string, mixed> $criteria
|
||||
* @psalm-param LockMode::* $lockMode
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function lock(array $criteria, $lockMode);
|
||||
|
||||
/**
|
||||
* Returns an array with (sliced or full list) of elements in the specified collection.
|
||||
*
|
||||
* @param object $sourceEntity
|
||||
* @param int|null $offset
|
||||
* @param int|null $limit
|
||||
* @psalm-param AssociationMapping $assoc
|
||||
*
|
||||
* @return mixed[]
|
||||
*/
|
||||
public function getOneToManyCollection(array $assoc, $sourceEntity, $offset = null, $limit = null);
|
||||
|
||||
/**
|
||||
* Checks whether the given managed entity exists in the database.
|
||||
*
|
||||
* @param object $entity
|
||||
*
|
||||
* @return bool TRUE if the entity exists in the database, FALSE otherwise.
|
||||
*/
|
||||
public function exists($entity, ?Criteria $extraConditions = null);
|
||||
}
|
||||
@@ -0,0 +1,650 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Persisters\Entity;
|
||||
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
use Doctrine\DBAL\LockMode;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Internal\SQLResultCasing;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use Doctrine\ORM\Utility\LockSqlHelper;
|
||||
use Doctrine\ORM\Utility\PersisterHelper;
|
||||
use LengthException;
|
||||
|
||||
use function array_combine;
|
||||
use function array_keys;
|
||||
use function array_values;
|
||||
use function implode;
|
||||
|
||||
/**
|
||||
* The joined subclass persister maps a single entity instance to several tables in the
|
||||
* database as it is defined by the <tt>Class Table Inheritance</tt> strategy.
|
||||
*
|
||||
* @see https://martinfowler.com/eaaCatalog/classTableInheritance.html
|
||||
*/
|
||||
class JoinedSubclassPersister extends AbstractEntityInheritancePersister
|
||||
{
|
||||
use LockSqlHelper;
|
||||
use SQLResultCasing;
|
||||
|
||||
/**
|
||||
* Map that maps column names to the table names that own them.
|
||||
* This is mainly a temporary cache, used during a single request.
|
||||
*
|
||||
* @psalm-var array<string, string>
|
||||
*/
|
||||
private $owningTableMap = [];
|
||||
|
||||
/**
|
||||
* Map of table to quoted table names.
|
||||
*
|
||||
* @psalm-var array<string, string>
|
||||
*/
|
||||
private $quotedTableMap = [];
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getDiscriminatorColumnTableName()
|
||||
{
|
||||
$class = $this->class->name !== $this->class->rootEntityName
|
||||
? $this->em->getClassMetadata($this->class->rootEntityName)
|
||||
: $this->class;
|
||||
|
||||
return $class->getTableName();
|
||||
}
|
||||
|
||||
/**
|
||||
* This function finds the ClassMetadata instance in an inheritance hierarchy
|
||||
* that is responsible for enabling versioning.
|
||||
*/
|
||||
private function getVersionedClassMetadata(): ClassMetadata
|
||||
{
|
||||
if (isset($this->class->fieldMappings[$this->class->versionField]['inherited'])) {
|
||||
$definingClassName = $this->class->fieldMappings[$this->class->versionField]['inherited'];
|
||||
|
||||
return $this->em->getClassMetadata($definingClassName);
|
||||
}
|
||||
|
||||
return $this->class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the table that owns the column the given field is mapped to.
|
||||
*
|
||||
* @param string $fieldName
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getOwningTable($fieldName)
|
||||
{
|
||||
if (isset($this->owningTableMap[$fieldName])) {
|
||||
return $this->owningTableMap[$fieldName];
|
||||
}
|
||||
|
||||
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();
|
||||
$quotedTableName = $this->quoteStrategy->getTableName($cm, $this->platform);
|
||||
|
||||
$this->owningTableMap[$fieldName] = $tableName;
|
||||
$this->quotedTableMap[$tableName] = $quotedTableName;
|
||||
|
||||
return $tableName;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function executeInserts()
|
||||
{
|
||||
if (! $this->queuedInserts) {
|
||||
return;
|
||||
}
|
||||
|
||||
$uow = $this->em->getUnitOfWork();
|
||||
$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
|
||||
$rootPersister = $this->em->getUnitOfWork()->getEntityPersister($rootClass->name);
|
||||
$rootTableName = $rootClass->getTableName();
|
||||
$rootTableStmt = $this->conn->prepare($rootPersister->getInsertSQL());
|
||||
|
||||
// Prepare statements for sub tables.
|
||||
$subTableStmts = [];
|
||||
|
||||
if ($rootClass !== $this->class) {
|
||||
$subTableStmts[$this->class->getTableName()] = $this->conn->prepare($this->getInsertSQL());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
// Execute insert on root table
|
||||
$paramIndex = 1;
|
||||
|
||||
foreach ($insertData[$rootTableName] as $columnName => $value) {
|
||||
$rootTableStmt->bindValue($paramIndex++, $value, $this->columnTypes[$columnName]);
|
||||
}
|
||||
|
||||
$rootTableStmt->executeStatement();
|
||||
|
||||
if ($isPostInsertId) {
|
||||
$generatedId = $idGenerator->generateId($this->em, $entity);
|
||||
$id = [$this->class->identifier[0] => $generatedId];
|
||||
|
||||
$uow->assignPostInsertId($entity, $generatedId);
|
||||
} else {
|
||||
$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) {
|
||||
$paramIndex = 1;
|
||||
$data = $insertData[$tableName] ?? [];
|
||||
|
||||
foreach ($id as $idName => $idVal) {
|
||||
$type = $this->columnTypes[$idName] ?? Types::STRING;
|
||||
|
||||
$stmt->bindValue($paramIndex++, $idVal, $type);
|
||||
}
|
||||
|
||||
foreach ($data as $columnName => $value) {
|
||||
if (! isset($id[$columnName])) {
|
||||
$stmt->bindValue($paramIndex++, $value, $this->columnTypes[$columnName]);
|
||||
}
|
||||
}
|
||||
|
||||
$stmt->executeStatement();
|
||||
}
|
||||
|
||||
if ($this->class->requiresFetchAfterChange) {
|
||||
$this->assignDefaultVersionAndUpsertableValues($entity, $id);
|
||||
}
|
||||
}
|
||||
|
||||
$this->queuedInserts = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function update($entity)
|
||||
{
|
||||
$updateData = $this->prepareUpdateData($entity);
|
||||
|
||||
if (! $updateData) {
|
||||
return;
|
||||
}
|
||||
|
||||
$isVersioned = $this->class->isVersioned;
|
||||
|
||||
$versionedClass = $this->getVersionedClassMetadata();
|
||||
$versionedTable = $versionedClass->getTableName();
|
||||
|
||||
foreach ($updateData as $tableName => $data) {
|
||||
$tableName = $this->quotedTableMap[$tableName];
|
||||
$versioned = $isVersioned && $versionedTable === $tableName;
|
||||
|
||||
$this->updateTable($entity, $tableName, $data, $versioned);
|
||||
}
|
||||
|
||||
if ($this->class->requiresFetchAfterChange) {
|
||||
// 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);
|
||||
|
||||
$this->updateTable($entity, $tableName, [], true);
|
||||
}
|
||||
|
||||
$identifiers = $this->em->getUnitOfWork()->getEntityIdentifier($entity);
|
||||
|
||||
$this->assignDefaultVersionAndUpsertableValues($entity, $identifiers);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function delete($entity)
|
||||
{
|
||||
$identifier = $this->em->getUnitOfWork()->getEntityIdentifier($entity);
|
||||
$id = array_combine($this->class->getIdentifierColumnNames(), $identifier);
|
||||
$types = $this->getClassIdentifiersTypes($this->class);
|
||||
|
||||
$this->deleteJoinTableRecords($identifier, $types);
|
||||
|
||||
// If the database platform supports FKs, just
|
||||
// delete the row from the root table. Cascades do the rest.
|
||||
if ($this->platform->supportsForeignKeyConstraints()) {
|
||||
$rootClass = $this->em->getClassMetadata($this->class->rootEntityName);
|
||||
$rootTable = $this->quoteStrategy->getTableName($rootClass, $this->platform);
|
||||
$rootTypes = $this->getClassIdentifiersTypes($rootClass);
|
||||
|
||||
return (bool) $this->conn->delete($rootTable, $id, $rootTypes);
|
||||
}
|
||||
|
||||
// Delete from all tables individually, starting from this class' table up to the root table.
|
||||
$rootTable = $this->quoteStrategy->getTableName($this->class, $this->platform);
|
||||
$rootTypes = $this->getClassIdentifiersTypes($this->class);
|
||||
|
||||
$affectedRows = $this->conn->delete($rootTable, $id, $rootTypes);
|
||||
|
||||
foreach ($this->class->parentClasses as $parentClass) {
|
||||
$parentMetadata = $this->em->getClassMetadata($parentClass);
|
||||
$parentTable = $this->quoteStrategy->getTableName($parentMetadata, $this->platform);
|
||||
$parentTypes = $this->getClassIdentifiersTypes($parentMetadata);
|
||||
|
||||
$this->conn->delete($parentTable, $id, $parentTypes);
|
||||
}
|
||||
|
||||
return (bool) $affectedRows;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getSelectSQL($criteria, $assoc = null, $lockMode = null, $limit = null, $offset = null, ?array $orderBy = null)
|
||||
{
|
||||
$this->switchPersisterContext($offset, $limit);
|
||||
|
||||
$baseTableAlias = $this->getSQLTableAlias($this->class->name);
|
||||
$joinSql = $this->getJoinSql($baseTableAlias);
|
||||
|
||||
if ($assoc !== null && $assoc['type'] === ClassMetadata::MANY_TO_MANY) {
|
||||
$joinSql .= $this->getSelectManyToManyJoinSQL($assoc);
|
||||
}
|
||||
|
||||
$conditionSql = $criteria instanceof Criteria
|
||||
? $this->getSelectConditionCriteriaSQL($criteria)
|
||||
: $this->getSelectConditionSQL($criteria, $assoc);
|
||||
|
||||
$filterSql = $this->generateFilterConditionSQL(
|
||||
$this->em->getClassMetadata($this->class->rootEntityName),
|
||||
$this->getSQLTableAlias($this->class->rootEntityName)
|
||||
);
|
||||
// If the current class in the root entity, add the filters
|
||||
if ($filterSql) {
|
||||
$conditionSql .= $conditionSql
|
||||
? ' AND ' . $filterSql
|
||||
: $filterSql;
|
||||
}
|
||||
|
||||
$orderBySql = '';
|
||||
|
||||
if ($assoc !== null && isset($assoc['orderBy'])) {
|
||||
$orderBy = $assoc['orderBy'];
|
||||
}
|
||||
|
||||
if ($orderBy) {
|
||||
$orderBySql = $this->getOrderBySQL($orderBy, $baseTableAlias);
|
||||
}
|
||||
|
||||
$lockSql = '';
|
||||
|
||||
switch ($lockMode) {
|
||||
case LockMode::PESSIMISTIC_READ:
|
||||
$lockSql = ' ' . $this->getReadLockSQL($this->platform);
|
||||
|
||||
break;
|
||||
|
||||
case LockMode::PESSIMISTIC_WRITE:
|
||||
$lockSql = ' ' . $this->getWriteLockSQL($this->platform);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
$tableName = $this->quoteStrategy->getTableName($this->class, $this->platform);
|
||||
$from = ' FROM ' . $tableName . ' ' . $baseTableAlias;
|
||||
$where = $conditionSql !== '' ? ' WHERE ' . $conditionSql : '';
|
||||
$lock = $this->platform->appendLockHint($from, $lockMode ?? LockMode::NONE);
|
||||
$columnList = $this->getSelectColumnsSQL();
|
||||
$query = 'SELECT ' . $columnList
|
||||
. $lock
|
||||
. $joinSql
|
||||
. $where
|
||||
. $orderBySql;
|
||||
|
||||
return $this->platform->modifyLimitQuery($query, $limit, $offset ?? 0) . $lockSql;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getCountSQL($criteria = [])
|
||||
{
|
||||
$tableName = $this->quoteStrategy->getTableName($this->class, $this->platform);
|
||||
$baseTableAlias = $this->getSQLTableAlias($this->class->name);
|
||||
$joinSql = $this->getJoinSql($baseTableAlias);
|
||||
|
||||
$conditionSql = $criteria instanceof Criteria
|
||||
? $this->getSelectConditionCriteriaSQL($criteria)
|
||||
: $this->getSelectConditionSQL($criteria);
|
||||
|
||||
$filterSql = $this->generateFilterConditionSQL($this->em->getClassMetadata($this->class->rootEntityName), $this->getSQLTableAlias($this->class->rootEntityName));
|
||||
|
||||
if ($filterSql !== '') {
|
||||
$conditionSql = $conditionSql
|
||||
? $conditionSql . ' AND ' . $filterSql
|
||||
: $filterSql;
|
||||
}
|
||||
|
||||
return 'SELECT COUNT(*) '
|
||||
. 'FROM ' . $tableName . ' ' . $baseTableAlias
|
||||
. $joinSql
|
||||
. (empty($conditionSql) ? '' : ' WHERE ' . $conditionSql);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getLockTablesSql($lockMode)
|
||||
{
|
||||
$joinSql = '';
|
||||
$identifierColumns = $this->class->getIdentifierColumnNames();
|
||||
$baseTableAlias = $this->getSQLTableAlias($this->class->name);
|
||||
|
||||
// INNER JOIN parent tables
|
||||
foreach ($this->class->parentClasses as $parentClassName) {
|
||||
$conditions = [];
|
||||
$tableAlias = $this->getSQLTableAlias($parentClassName);
|
||||
$parentClass = $this->em->getClassMetadata($parentClassName);
|
||||
$joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON ';
|
||||
|
||||
foreach ($identifierColumns as $idColumn) {
|
||||
$conditions[] = $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
||||
}
|
||||
|
||||
$joinSql .= implode(' AND ', $conditions);
|
||||
}
|
||||
|
||||
return parent::getLockTablesSql($lockMode) . $joinSql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure this method is never called. This persister overrides getSelectEntitiesSQL directly.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getSelectColumnsSQL()
|
||||
{
|
||||
// Create the column list fragment only once
|
||||
if ($this->currentPersisterContext->selectColumnListSql !== null) {
|
||||
return $this->currentPersisterContext->selectColumnListSql;
|
||||
}
|
||||
|
||||
$columnList = [];
|
||||
$discrColumn = $this->class->getDiscriminatorColumn();
|
||||
$discrColumnName = $discrColumn['name'];
|
||||
$discrColumnType = $discrColumn['type'];
|
||||
$baseTableAlias = $this->getSQLTableAlias($this->class->name);
|
||||
$resultColumnName = $this->getSQLResultCasing($this->platform, $discrColumnName);
|
||||
|
||||
$this->currentPersisterContext->rsm->addEntityResult($this->class->name, 'r');
|
||||
$this->currentPersisterContext->rsm->setDiscriminatorColumn('r', $resultColumnName);
|
||||
$this->currentPersisterContext->rsm->addMetaResult('r', $resultColumnName, $discrColumnName, false, $discrColumnType);
|
||||
|
||||
// Add regular columns
|
||||
foreach ($this->class->fieldMappings as $fieldName => $mapping) {
|
||||
$class = isset($mapping['inherited'])
|
||||
? $this->em->getClassMetadata($mapping['inherited'])
|
||||
: $this->class;
|
||||
|
||||
$columnList[] = $this->getSelectColumnSQL($fieldName, $class);
|
||||
}
|
||||
|
||||
// Add foreign key columns
|
||||
foreach ($this->class->associationMappings as $mapping) {
|
||||
if (! $mapping['isOwningSide'] || ! ($mapping['type'] & ClassMetadata::TO_ONE)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tableAlias = isset($mapping['inherited'])
|
||||
? $this->getSQLTableAlias($mapping['inherited'])
|
||||
: $baseTableAlias;
|
||||
|
||||
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
|
||||
|
||||
foreach ($mapping['joinColumns'] as $joinColumn) {
|
||||
$columnList[] = $this->getSelectJoinColumnSQL(
|
||||
$tableAlias,
|
||||
$joinColumn['name'],
|
||||
$this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform),
|
||||
PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $this->em)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Add discriminator column (DO NOT ALIAS, see AbstractEntityInheritancePersister#processSQLResult).
|
||||
$tableAlias = $this->class->rootEntityName === $this->class->name
|
||||
? $baseTableAlias
|
||||
: $this->getSQLTableAlias($this->class->rootEntityName);
|
||||
|
||||
$columnList[] = $tableAlias . '.' . $discrColumnName;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
|
||||
|
||||
foreach ($mapping['joinColumns'] as $joinColumn) {
|
||||
$columnList[] = $this->getSelectJoinColumnSQL(
|
||||
$tableAlias,
|
||||
$joinColumn['name'],
|
||||
$this->quoteStrategy->getJoinColumnName($joinColumn, $subClass, $this->platform),
|
||||
PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $this->em)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->currentPersisterContext->selectColumnListSql = implode(', ', $columnList);
|
||||
|
||||
return $this->currentPersisterContext->selectColumnListSql;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getInsertColumnList()
|
||||
{
|
||||
// Identifier columns must always come first in the column list of subclasses.
|
||||
$columns = $this->class->parentClasses
|
||||
? $this->class->getIdentifierColumnNames()
|
||||
: [];
|
||||
|
||||
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)
|
||||
|| isset($this->class->embeddedClasses[$name])
|
||||
|| isset($this->class->fieldMappings[$name]['notInsertable'])
|
||||
) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
} elseif (
|
||||
$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->getDiscriminatorColumn()['name'];
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function assignDefaultVersionAndUpsertableValues($entity, array $id)
|
||||
{
|
||||
$values = $this->fetchVersionAndNotUpsertableValues($this->getVersionedClassMetadata(), $id);
|
||||
|
||||
foreach ($values as $field => $value) {
|
||||
$value = Type::getType($this->class->fieldMappings[$field]['type'])->convertToPHPValue($value, $this->platform);
|
||||
|
||||
$this->class->setFieldValue($entity, $field, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function fetchVersionAndNotUpsertableValues($versionedClass, array $id)
|
||||
{
|
||||
$columnNames = [];
|
||||
foreach ($this->class->fieldMappings as $key => $column) {
|
||||
$class = null;
|
||||
if ($this->class->isVersioned && $key === $versionedClass->versionField) {
|
||||
$class = $versionedClass;
|
||||
} elseif (isset($column['generated'])) {
|
||||
$class = isset($column['inherited'])
|
||||
? $this->em->getClassMetadata($column['inherited'])
|
||||
: $this->class;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
$columnNames[$key] = $this->getSelectColumnSQL($key, $class);
|
||||
}
|
||||
|
||||
$tableName = $this->quoteStrategy->getTableName($versionedClass, $this->platform);
|
||||
$baseTableAlias = $this->getSQLTableAlias($this->class->name);
|
||||
$joinSql = $this->getJoinSql($baseTableAlias);
|
||||
$identifier = $this->quoteStrategy->getIdentifierColumnNames($versionedClass, $this->platform);
|
||||
foreach ($identifier as $i => $idValue) {
|
||||
$identifier[$i] = $baseTableAlias . '.' . $idValue;
|
||||
}
|
||||
|
||||
$sql = 'SELECT ' . implode(', ', $columnNames)
|
||||
. ' FROM ' . $tableName . ' ' . $baseTableAlias
|
||||
. $joinSql
|
||||
. ' WHERE ' . implode(' = ? AND ', $identifier) . ' = ?';
|
||||
|
||||
$flatId = $this->identifierFlattener->flattenIdentifier($versionedClass, $id);
|
||||
$values = $this->conn->fetchNumeric(
|
||||
$sql,
|
||||
array_values($flatId),
|
||||
$this->extractIdentifierTypes($id, $versionedClass)
|
||||
);
|
||||
|
||||
if ($values === false) {
|
||||
throw new LengthException('Unexpected empty result for database query.');
|
||||
}
|
||||
|
||||
$values = array_combine(array_keys($columnNames), $values);
|
||||
|
||||
if (! $values) {
|
||||
throw new LengthException('Unexpected number of database columns.');
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
private function getJoinSql(string $baseTableAlias): string
|
||||
{
|
||||
$joinSql = '';
|
||||
$identifierColumn = $this->class->getIdentifierColumnNames();
|
||||
|
||||
// INNER JOIN parent tables
|
||||
foreach ($this->class->parentClasses as $parentClassName) {
|
||||
$conditions = [];
|
||||
$parentClass = $this->em->getClassMetadata($parentClassName);
|
||||
$tableAlias = $this->getSQLTableAlias($parentClassName);
|
||||
$joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON ';
|
||||
|
||||
foreach ($identifierColumn as $idColumn) {
|
||||
$conditions[] = $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
||||
}
|
||||
|
||||
$joinSql .= implode(' AND ', $conditions);
|
||||
}
|
||||
|
||||
// OUTER JOIN sub tables
|
||||
foreach ($this->class->subClasses as $subClassName) {
|
||||
$conditions = [];
|
||||
$subClass = $this->em->getClassMetadata($subClassName);
|
||||
$tableAlias = $this->getSQLTableAlias($subClassName);
|
||||
$joinSql .= ' LEFT JOIN ' . $this->quoteStrategy->getTableName($subClass, $this->platform) . ' ' . $tableAlias . ' ON ';
|
||||
|
||||
foreach ($identifierColumn as $idColumn) {
|
||||
$conditions[] = $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
||||
}
|
||||
|
||||
$joinSql .= implode(' AND ', $conditions);
|
||||
}
|
||||
|
||||
return $joinSql;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Persisters\Entity;
|
||||
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
use Doctrine\ORM\Internal\SQLResultCasing;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use Doctrine\ORM\Utility\PersisterHelper;
|
||||
|
||||
use function array_flip;
|
||||
use function array_intersect;
|
||||
use function array_map;
|
||||
use function array_unshift;
|
||||
use function implode;
|
||||
|
||||
/**
|
||||
* Persister for entities that participate in a hierarchy mapped with the
|
||||
* SINGLE_TABLE strategy.
|
||||
*
|
||||
* @link https://martinfowler.com/eaaCatalog/singleTableInheritance.html
|
||||
*/
|
||||
class SingleTablePersister extends AbstractEntityInheritancePersister
|
||||
{
|
||||
use SQLResultCasing;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getDiscriminatorColumnTableName()
|
||||
{
|
||||
return $this->class->getTableName();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getSelectColumnsSQL()
|
||||
{
|
||||
if ($this->currentPersisterContext->selectColumnListSql !== null) {
|
||||
return $this->currentPersisterContext->selectColumnListSql;
|
||||
}
|
||||
|
||||
$columnList[] = parent::getSelectColumnsSQL();
|
||||
|
||||
$rootClass = $this->em->getClassMetadata($this->class->rootEntityName);
|
||||
$tableAlias = $this->getSQLTableAlias($rootClass->name);
|
||||
|
||||
// Append discriminator column
|
||||
$discrColumn = $this->class->getDiscriminatorColumn();
|
||||
$discrColumnName = $discrColumn['name'];
|
||||
$discrColumnType = $discrColumn['type'];
|
||||
|
||||
$columnList[] = $tableAlias . '.' . $discrColumnName;
|
||||
|
||||
$resultColumnName = $this->getSQLResultCasing($this->platform, $discrColumnName);
|
||||
|
||||
$this->currentPersisterContext->rsm->setDiscriminatorColumn('r', $resultColumnName);
|
||||
$this->currentPersisterContext->rsm->addMetaResult('r', $resultColumnName, $discrColumnName, false, $discrColumnType);
|
||||
|
||||
// Append subclass columns
|
||||
foreach ($this->class->subClasses as $subClassName) {
|
||||
$subClass = $this->em->getClassMetadata($subClassName);
|
||||
|
||||
// Regular columns
|
||||
foreach ($subClass->fieldMappings as $fieldName => $mapping) {
|
||||
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'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
|
||||
|
||||
foreach ($assoc['joinColumns'] as $joinColumn) {
|
||||
$columnList[] = $this->getSelectJoinColumnSQL(
|
||||
$tableAlias,
|
||||
$joinColumn['name'],
|
||||
$this->quoteStrategy->getJoinColumnName($joinColumn, $subClass, $this->platform),
|
||||
PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $this->em)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->currentPersisterContext->selectColumnListSql = implode(', ', $columnList);
|
||||
|
||||
return $this->currentPersisterContext->selectColumnListSql;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getInsertColumnList()
|
||||
{
|
||||
$columns = parent::getInsertColumnList();
|
||||
|
||||
// Add discriminator column to the INSERT SQL
|
||||
$columns[] = $this->class->getDiscriminatorColumn()['name'];
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getSQLTableAlias($className, $assocName = '')
|
||||
{
|
||||
return parent::getSQLTableAlias($this->class->rootEntityName, $assocName);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getSelectConditionSQL(array $criteria, $assoc = null)
|
||||
{
|
||||
$conditionSql = parent::getSelectConditionSQL($criteria, $assoc);
|
||||
|
||||
if ($conditionSql) {
|
||||
$conditionSql .= ' AND ';
|
||||
}
|
||||
|
||||
return $conditionSql . $this->getSelectConditionDiscriminatorValueSQL();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getSelectConditionCriteriaSQL(Criteria $criteria)
|
||||
{
|
||||
$conditionSql = parent::getSelectConditionCriteriaSQL($criteria);
|
||||
|
||||
if ($conditionSql) {
|
||||
$conditionSql .= ' AND ';
|
||||
}
|
||||
|
||||
return $conditionSql . $this->getSelectConditionDiscriminatorValueSQL();
|
||||
}
|
||||
|
||||
/** @return string */
|
||||
protected function getSelectConditionDiscriminatorValueSQL()
|
||||
{
|
||||
$values = array_map(
|
||||
[$this->conn, 'quote'],
|
||||
array_flip(array_intersect($this->class->discriminatorMap, $this->class->subClasses))
|
||||
);
|
||||
|
||||
if ($this->class->discriminatorValue !== null) { // discriminators can be 0
|
||||
array_unshift($values, $this->conn->quote($this->class->discriminatorValue));
|
||||
}
|
||||
|
||||
$discColumnName = $this->class->getDiscriminatorColumn()['name'];
|
||||
|
||||
$values = implode(', ', $values);
|
||||
$tableAlias = $this->getSQLTableAlias($this->class->name);
|
||||
|
||||
return $tableAlias . '.' . $discColumnName . ' IN (' . $values . ')';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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);
|
||||
// we don't care about the $targetTableAlias, in a STI there is only one table.
|
||||
|
||||
return parent::generateFilterConditionSQL($targetEntity, $targetTableAlias);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user