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 1 commit
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
Prev Previous commit
Next Next commit
Fix psalm
  • Loading branch information
VincentLanglet committed Nov 7, 2021
commit 82de8f845e029701898c21eb95071d4302d3151d
5 changes: 4 additions & 1 deletion lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use function array_keys;
use function array_search;
use function assert;
use function count;
use function in_array;
use function key;
Expand Down Expand Up @@ -79,7 +80,9 @@ 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->discriminatorColumn;
assert($discrColumn !== null);
$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
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ protected function 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;
if ($discColumn !== null) {
$this->columnTypes[$discColumn['name']] = $discColumn['type'];
$data[$this->getDiscriminatorColumnTableName()][$discColumn['name']] = $this->class->discriminatorValue;
}

return $data;
}
Expand Down
19 changes: 13 additions & 6 deletions lib/Doctrine/ORM/Persisters/Entity/JoinedSubclassPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\ORM\Utility\PersisterHelper;

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

Expand Down Expand Up @@ -411,15 +412,18 @@ protected function getSelectColumnsSQL()
return $this->currentPersisterContext->selectColumnListSql;
}

$discrColumn = $this->class->discriminatorColumn;
assert($discrColumn !== null);

$columnList = [];
$discrColumn = $this->class->discriminatorColumn['name'];
$discrColumnType = $this->class->discriminatorColumn['type'];
$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 +461,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 +544,10 @@ protected function getInsertColumnList()

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

$columns[] = $discrColumn['name'];
}

return $columns;
Expand Down
26 changes: 18 additions & 8 deletions lib/Doctrine/ORM/Persisters/Entity/SingleTablePersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\ORM\Utility\PersisterHelper;

use function array_flip;
use function assert;
use function implode;

/**
Expand Down Expand Up @@ -44,16 +45,19 @@ protected function getSelectColumnsSQL()
$rootClass = $this->em->getClassMetadata($this->class->rootEntityName);
$tableAlias = $this->getSQLTableAlias($rootClass->name);

$discrColumn = $this->class->discriminatorColumn;
assert($discrColumn !== null);

// Append discriminator column
$discrColumn = $this->class->discriminatorColumn['name'];
$discrColumnType = $this->class->discriminatorColumn['type'];
$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 @@ -99,8 +103,11 @@ protected function getInsertColumnList()
{
$columns = parent::getInsertColumnList();

$discrColumn = $this->class->discriminatorColumn;
assert($discrColumn !== null);

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

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

$discrColumn = $this->class->discriminatorColumn;
assert($discrColumn !== null);
$discColumnName = $discrColumn['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
6 changes: 5 additions & 1 deletion lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\ORM\Utility\PersisterHelper;
use InvalidArgumentException;

use function assert;
use function explode;
use function in_array;
use function sprintf;
Expand Down Expand Up @@ -369,7 +370,10 @@ public function addNamedNativeQueryEntityResultMapping(ClassMetadataInfo $classM
{
if (isset($entityMapping['discriminatorColumn']) && $entityMapping['discriminatorColumn']) {
$discriminatorColumn = $entityMapping['discriminatorColumn'];
$discriminatorType = $classMetadata->discriminatorColumn['type'];

$classMetadataDiscrColumn = $classMetadata->discriminatorColumn;
assert($classMetadataDiscrColumn !== null);
$discriminatorType = $classMetadataDiscrColumn['type'];

$this->setDiscriminatorColumn($alias, $discriminatorColumn);
$this->addMetaResult($alias, $discriminatorColumn, $discriminatorColumn, false, $discriminatorType);
Expand Down
12 changes: 10 additions & 2 deletions lib/Doctrine/ORM/Query/SqlWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use function array_keys;
use function array_map;
use function array_merge;
use function assert;
use function count;
use function implode;
use function in_array;
Expand Down Expand Up @@ -465,7 +466,10 @@ private function generateDiscriminatorColumnConditionSQL(array $dqlAliases): str
? $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.'
: '';

$sqlParts[] = $sqlTableAlias . $class->discriminatorColumn['name'] . ' IN (' . implode(', ', $values) . ')';
$discrColumn = $class->discriminatorColumn;
assert($discrColumn !== null);

$sqlParts[] = $sqlTableAlias . $discrColumn['name'] . ' IN (' . implode(', ', $values) . ')';
}

$sql = implode(' AND ', $sqlParts);
Expand Down Expand Up @@ -740,6 +744,7 @@ public function walkSelectClause($selectClause)
$rootClass = $this->em->getClassMetadata($class->rootEntityName);
$tblAlias = $this->getSQLTableAlias($rootClass->getTableName(), $dqlAlias);
$discrColumn = $rootClass->discriminatorColumn;
assert($discrColumn !== null);
$columnAlias = $this->getSQLColumnAlias($discrColumn['name']);

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

$sql .= $class->discriminatorColumn['name'] . ($instanceOfExpr->not ? ' NOT IN ' : ' IN ');
$discrColumn = $class->discriminatorColumn;
assert($discrColumn !== null);

$sql .= $discrColumn['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