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 ArrayHydration of enums #10041

Merged
merged 1 commit into from
Sep 22, 2022
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
5 changes: 5 additions & 0 deletions lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,10 @@ protected function gatherRowData(array $data, array &$id, array &$nonemptyCompon
break;
}

if ($value !== null && isset($cacheKeyInfo['enumType'])) {
$value = $this->buildEnum($value, $cacheKeyInfo['enumType']);
}

$rowData['data'][$dqlAlias][$fieldName] = $type
? $type->convertToPHPValue($value, $this->_platform)
: $value;
Expand Down Expand Up @@ -547,6 +551,7 @@ protected function hydrateColumnInfo($key)
'fieldName' => $fieldName,
'type' => Type::getType($fieldMapping['type']),
'dqlAlias' => $ownerMap,
'enumType' => $this->_rsm->enumMappings[$key] ?? null,
];

// the current discriminator value must be saved in order to disambiguate fields hydration,
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/ORM/Query/SqlWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,10 @@ public function walkSelectExpression($selectExpression)
$this->scalarResultAliasMap[$resultAlias][] = $columnAlias;

$this->rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $class->name);

if (! empty($mapping['enumType'])) {
$this->rsm->addEnumResult($columnAlias, $mapping['enumType']);
}
}

// Add any additional fields of subclasses (excluding inherited fields)
Expand Down
23 changes: 23 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\MappingException;
Expand Down Expand Up @@ -86,6 +87,28 @@ public function testEnumHydration(): void
$this->assertEquals(Suit::Clubs, $result[0]['suit']);
}

public function testEnumHydrationArrayHydrator(): void
{
$this->setUpEntitySchema([Card::class, CardWithNullable::class]);

$card = new Card();
$card->suit = Suit::Clubs;

$this->_em->persist($card);
$this->_em->flush();
$this->_em->clear();

$result = $this->_em->createQueryBuilder()
->from(Card::class, 'c')
->select('c')
->getQuery()
//->getResult();
->getResult(AbstractQuery::HYDRATE_ARRAY);

$this->assertInstanceOf(Suit::class, $result[0]['suit']);
$this->assertEquals(Suit::Clubs, $result[0]['suit']);
}

public function testNullableEnumHydration(): void
{
$this->setUpEntitySchema([Card::class, CardWithNullable::class]);
Expand Down