Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix discriminatorColumn phpdoc #9168

Merged
merged 3 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ protected function hydrateRowData(array $row, array &$result)

// We need to find the correct entity class name if we have inheritance in resultset
if ($this->class->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
$discrColumnName = $this->getSQLResultCasing($this->_platform, $this->class->discriminatorColumn['name']);
$discrColumn = $this->class->getDiscriminatorColumn();
$discrColumnName = $this->getSQLResultCasing($this->_platform, $discrColumn['name']);

// Find mapped discriminator column from the result set.
$metaMappingDiscrColumnName = array_search($discrColumnName, $this->resultSetMapping()->metaMappings, true);
Expand Down
15 changes: 14 additions & 1 deletion lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\ReflectionService;
use InvalidArgumentException;
use LogicException;
use ReflectionClass;
use ReflectionNamedType;
use ReflectionProperty;
Expand Down Expand Up @@ -496,7 +497,7 @@ class ClassMetadataInfo implements ClassMetadata
* READ-ONLY: The definition of the discriminator column used in JOINED and SINGLE_TABLE
* inheritance mappings.
*
* @psalm-var array<string, mixed>
* @psalm-var array<string, mixed>|null
*/
public $discriminatorColumn;

Expand Down Expand Up @@ -3098,6 +3099,18 @@ public function setDiscriminatorColumn($columnDef)
}
}

/**
* @return array<string, mixed>
*/
final public function getDiscriminatorColumn(): array
{
if ($this->discriminatorColumn === null) {
throw new LogicException('The discriminator column was not set.');
}

return $this->discriminatorColumn;
}

/**
* Sets the discriminator values used by this class.
* Used for JOINED and SINGLE_TABLE inheritance mapping strategies.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function prepareInsertData($entity)
$data = parent::prepareInsertData($entity);

// Populate the discriminator column
$discColumn = $this->class->discriminatorColumn;
$discColumn = $this->class->getDiscriminatorColumn();
$this->columnTypes[$discColumn['name']] = $discColumn['type'];
$data[$this->getDiscriminatorColumnTableName()][$discColumn['name']] = $this->class->discriminatorValue;

Expand Down
14 changes: 7 additions & 7 deletions lib/Doctrine/ORM/Persisters/Entity/JoinedSubclassPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use function array_combine;
use function implode;
use function is_array;

/**
* The joined subclass persister maps a single entity instance to several tables in the
Expand Down Expand Up @@ -412,14 +411,15 @@ protected function getSelectColumnsSQL()
}

$columnList = [];
$discrColumn = $this->class->discriminatorColumn['name'];
$discrColumnType = $this->class->discriminatorColumn['type'];
$discrColumn = $this->class->getDiscriminatorColumn();
$discrColumnName = $discrColumn['name'];
$discrColumnType = $discrColumn['type'];
$baseTableAlias = $this->getSQLTableAlias($this->class->name);
$resultColumnName = $this->getSQLResultCasing($this->platform, $discrColumn);
$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, $discrColumn, false, $discrColumnType);
$this->currentPersisterContext->rsm->addMetaResult('r', $resultColumnName, $discrColumnName, false, $discrColumnType);

// Add regular columns
foreach ($this->class->fieldMappings as $fieldName => $mapping) {
Expand Down Expand Up @@ -457,7 +457,7 @@ protected function getSelectColumnsSQL()
? $baseTableAlias
: $this->getSQLTableAlias($this->class->rootEntityName);

$columnList[] = $tableAlias . '.' . $discrColumn;
$columnList[] = $tableAlias . '.' . $discrColumnName;

// sub tables
foreach ($this->class->subClasses as $subClassName) {
Expand Down Expand Up @@ -540,7 +540,7 @@ protected function getInsertColumnList()

// Add discriminator column if it is the topmost class.
if ($this->class->name === $this->class->rootEntityName) {
$columns[] = $this->class->discriminatorColumn['name'];
$columns[] = $this->class->getDiscriminatorColumn()['name'];
}

return $columns;
Expand Down
20 changes: 11 additions & 9 deletions lib/Doctrine/ORM/Persisters/Entity/SingleTablePersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@ protected function getSelectColumnsSQL()
$rootClass = $this->em->getClassMetadata($this->class->rootEntityName);
$tableAlias = $this->getSQLTableAlias($rootClass->name);

// Append discriminator column
$discrColumn = $this->class->discriminatorColumn['name'];
$discrColumnType = $this->class->discriminatorColumn['type'];
// Append discriminator column
$discrColumn = $this->class->getDiscriminatorColumn();
$discrColumnName = $discrColumn['name'];
$discrColumnType = $discrColumn['type'];

$columnList[] = $tableAlias . '.' . $discrColumn;
$columnList[] = $tableAlias . '.' . $discrColumnName;

$resultColumnName = $this->getSQLResultCasing($this->platform, $discrColumn);
$resultColumnName = $this->getSQLResultCasing($this->platform, $discrColumnName);

$this->currentPersisterContext->rsm->setDiscriminatorColumn('r', $resultColumnName);
$this->currentPersisterContext->rsm->addMetaResult('r', $resultColumnName, $discrColumn, false, $discrColumnType);
$this->currentPersisterContext->rsm->addMetaResult('r', $resultColumnName, $discrColumnName, false, $discrColumnType);

// Append subclass columns
foreach ($this->class->subClasses as $subClassName) {
Expand Down Expand Up @@ -100,7 +101,7 @@ protected function getInsertColumnList()
$columns = parent::getInsertColumnList();

// Add discriminator column to the INSERT SQL
$columns[] = $this->class->discriminatorColumn['name'];
$columns[] = $this->class->getDiscriminatorColumn()['name'];

return $columns;
}
Expand Down Expand Up @@ -158,11 +159,12 @@ protected function getSelectConditionDiscriminatorValueSQL()
$values[] = $this->conn->quote($discrValues[$subclassName]);
}

$discColumnName = $this->class->getDiscriminatorColumn()['name'];

$values = implode(', ', $values);
$discColumn = $this->class->discriminatorColumn['name'];
$tableAlias = $this->getSQLTableAlias($this->class->name);

return $tableAlias . '.' . $discColumn . ' IN (' . $values . ')';
return $tableAlias . '.' . $discColumnName . ' IN (' . $values . ')';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public function addNamedNativeQueryEntityResultMapping(ClassMetadataInfo $classM
{
if (isset($entityMapping['discriminatorColumn']) && $entityMapping['discriminatorColumn']) {
$discriminatorColumn = $entityMapping['discriminatorColumn'];
$discriminatorType = $classMetadata->discriminatorColumn['type'];
$discriminatorType = $classMetadata->getDiscriminatorColumn()['type'];

$this->setDiscriminatorColumn($alias, $discriminatorColumn);
$this->addMetaResult($alias, $discriminatorColumn, $discriminatorColumn, false, $discriminatorType);
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/Query/SqlWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ private function generateDiscriminatorColumnConditionSQL(array $dqlAliases): str
? $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.'
: '';

$sqlParts[] = $sqlTableAlias . $class->discriminatorColumn['name'] . ' IN (' . implode(', ', $values) . ')';
$sqlParts[] = $sqlTableAlias . $class->getDiscriminatorColumn()['name'] . ' IN (' . implode(', ', $values) . ')';
}

$sql = implode(' AND ', $sqlParts);
Expand Down Expand Up @@ -739,7 +739,7 @@ public function walkSelectClause($selectClause)
// Add discriminator columns to SQL
$rootClass = $this->em->getClassMetadata($class->rootEntityName);
$tblAlias = $this->getSQLTableAlias($rootClass->getTableName(), $dqlAlias);
$discrColumn = $rootClass->discriminatorColumn;
$discrColumn = $rootClass->getDiscriminatorColumn();
$columnAlias = $this->getSQLColumnAlias($discrColumn['name']);

$sqlSelectExpressions[] = $tblAlias . '.' . $discrColumn['name'] . ' AS ' . $columnAlias;
Expand Down Expand Up @@ -2091,7 +2091,7 @@ public function walkInstanceOfExpression($instanceOfExpr)
$sql .= $this->getSQLTableAlias($discrClass->getTableName(), $dqlAlias) . '.';
}

$sql .= $class->discriminatorColumn['name'] . ($instanceOfExpr->not ? ' NOT IN ' : ' IN ');
$sql .= $class->getDiscriminatorColumn()['name'] . ($instanceOfExpr->not ? ' NOT IN ' : ' IN ');
$sql .= $this->getChildDiscriminatorsFromClassMetadata($discrClass, $instanceOfExpr);

return $sql;
Expand Down
6 changes: 5 additions & 1 deletion lib/Doctrine/ORM/Tools/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,11 @@ protected function generateDiscriminatorColumnAnnotation(ClassMetadataInfo $meta
return '';
}

$discrColumn = $metadata->discriminatorColumn;
$discrColumn = $metadata->discriminatorColumn;
if ($discrColumn === null) {
return '';
}

$columnDefinition = 'name="' . $discrColumn['name']
. '", type="' . $discrColumn['type']
. '", length=' . $discrColumn['length'];
Expand Down