Skip to content

Commit

Permalink
Fix using enums with the QueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed Sep 22, 2022
1 parent a8b02fd commit 42cc9c7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,14 +464,14 @@ 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;

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

if ($cacheKeyInfo['isIdentifier'] && $value !== null) {
$id[$dqlAlias] .= '|' . $value;
$nonemptyComponents[$dqlAlias] = true;
Expand Down
12 changes: 8 additions & 4 deletions lib/Doctrine/ORM/Mapping/ReflectionEnumProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public function getValue($object = null)
}

/**
* @param object $object
* @param int|string|int[]|string[]|null $value
* @param object $object
* @param int|string|int[]|string[]|BackedEnum|BackedEnum[]|null $value
*/
public function setValue($object, $value = null): void
{
Expand All @@ -82,11 +82,15 @@ public function setValue($object, $value = null): void
}

/**
* @param object $object
* @param int|string $value
* @param object $object
* @param int|string|BackedEnum $value
*/
private function initializeEnumValue($object, $value): BackedEnum
{
if ($value instanceof BackedEnum) {
return $value;
}

$enumType = $this->enumType;

try {
Expand Down
52 changes: 52 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,58 @@ public function testEnumMapping(string $cardClass): void
$this->assertEquals(Suit::Clubs, $fetchedCard->suit);
}

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

$card1 = new Card();
$card1->suit = Suit::Clubs;
$card2 = new Card();
$card2->suit = Suit::Hearts;

$this->_em->persist($card1);
$this->_em->persist($card2);
$this->_em->flush();

unset($card1, $card2);
$this->_em->clear();

/** @var list<Card> $foundCards */
$foundCards = $this->_em->createQueryBuilder()
->select('c')
->from(Card::class, 'c')
->where('c.suit = :suit')
->setParameter('suit', Suit::Clubs)
->getQuery()
->getResult();

$this->assertNotEmpty($foundCards);
foreach ($foundCards as $card) {
$this->assertSame(Suit::Clubs, $card->suit);
}
}

public function testEnumArrayHydrationObjectHydrator(): void
{
$this->setUpEntitySchema([Scale::class]);

$scale = new Scale();
$scale->supportedUnits = [Unit::Gram, Unit::Meter];

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

$result = $this->_em->createQueryBuilder()
->from(Scale::class, 's')
->select('s')
->getQuery()
->getResult();

self::assertInstanceOf(Scale::class, $result[0]);
self::assertEqualsCanonicalizing([Unit::Gram, Unit::Meter], $result[0]->supportedUnits);
}

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

0 comments on commit 42cc9c7

Please sign in to comment.