Skip to content

Commit

Permalink
Fix Hydration when use ManyToMany[indexBy] (#11783)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbannik committed Jan 17, 2025
1 parent 29a9783 commit 867f8c9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/Persisters/Entity/BasicEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -1560,7 +1560,15 @@ protected function getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r'
$tableAlias = $this->getSQLTableAlias($class->name, $root);
$fieldMapping = $class->fieldMappings[$field];
$sql = sprintf('%s.%s', $tableAlias, $this->quoteStrategy->getColumnName($field, $class, $this->platform));
$columnAlias = $this->getSQLColumnAlias($fieldMapping['columnName']);

$columnAlias = null;
if ($this->currentPersisterContext->rsm->hasColumnAliasByField($alias, $fieldMapping['columnName'])) {
$columnAlias = $this->currentPersisterContext->rsm->getColumnAliasByField($alias, $fieldMapping['columnName']);
}

if ($columnAlias === null) {
$columnAlias = $this->getSQLColumnAlias($fieldMapping['columnName']);
}

$this->currentPersisterContext->rsm->addFieldResult($alias, $columnAlias, $field);
if (! empty($fieldMapping['enumType'])) {
Expand Down
35 changes: 31 additions & 4 deletions src/Query/ResultSetMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ class ResultSetMapping
*/
public $fieldMappings = [];

/**
* Map field names for each class to alias
*
* @var array<class-string, array<string, array<string, string>>>
*/
public $columnAliasMappings = [];

/**
* Maps column names in the result set to the alias/field name to use in the mapped result.
*
Expand Down Expand Up @@ -315,7 +322,8 @@ public function isFieldResult($columnName)
/**
* Adds a field to the result that belongs to an entity or joined entity.
*
* @param string $alias The alias of the root entity or joined entity to which the field belongs.
* @param string $alias The alias of the root entity or joined entity to which the field
* belongs.
* @param string $columnName The name of the column in the SQL result set.
* @param string $fieldName The name of the field on the declaring class.
* @param class-string|null $declaringClass The name of the class that declares/owns the specified field.
Expand All @@ -335,7 +343,10 @@ public function addFieldResult($alias, $columnName, $fieldName, $declaringClass
// column name => alias of owner
$this->columnOwnerMap[$columnName] = $alias;
// field name => class name of declaring class
$this->declaringClasses[$columnName] = $declaringClass ?: $this->aliasMap[$alias];
$declaringClass = $declaringClass ?: $this->aliasMap[$alias];
$this->declaringClasses[$columnName] = $declaringClass;

$this->columnAliasMappings[$declaringClass][$alias][$fieldName] = $columnName;

if (! $this->isMixed && $this->scalarMappings) {
$this->isMixed = true;
Expand All @@ -344,6 +355,20 @@ public function addFieldResult($alias, $columnName, $fieldName, $declaringClass
return $this;
}

public function hasColumnAliasByField(string $alias, string $fieldName): bool
{
$declaringClass = $this->aliasMap[$alias];

return isset($this->columnAliasMappings[$declaringClass][$alias][$fieldName]);
}

public function getColumnAliasByField(string $alias, string $fieldName): string
{
$declaringClass = $this->aliasMap[$alias];

return $this->columnAliasMappings[$declaringClass][$alias][$fieldName];
}

/**
* Adds a joined entity result.
*
Expand All @@ -370,7 +395,8 @@ public function addJoinedEntityResult($class, $alias, $parentAlias, $relation)
* Adds a scalar result mapping.
*
* @param string $columnName The name of the column in the SQL result set.
* @param string|int $alias The result alias with which the scalar result should be placed in the result structure.
* @param string|int $alias The result alias with which the scalar result should be placed in the result
* structure.
* @param string $type The column type
*
* @return $this
Expand Down Expand Up @@ -570,7 +596,8 @@ public function isMixedResult()
/**
* Adds a meta column (foreign key or discriminator column) to the result set.
*
* @param string $alias The result alias with which the meta result should be placed in the result structure.
* @param string $alias The result alias with which the meta result should be placed in the
* result structure.
* @param string $columnName The name of the column in the SQL result set.
* @param string $fieldName The name of the field on the declaring class.
* @param bool $isIdentifierColumn
Expand Down

0 comments on commit 867f8c9

Please sign in to comment.