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 3fa2250
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/Mapping/ReflectionEnumProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public function getValue($object = null)
}

/**
* @param object $object
* @param int|string|int[]|string[]|null $value
* @param object $object
* @param int|string|int[]|string[]|BackedEnum|null $value
*/
public function setValue($object, $value = null): void
{
if ($value !== null) {
if ($value !== null && ! $value instanceof BackedEnum) {
if (is_array($value)) {
$value = array_map(function ($item) use ($object): BackedEnum {
return $this->initializeEnumValue($object, $item);
Expand Down
31 changes: 31 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,37 @@ public function testFindByEnum(): void
}
}

public function testQueryBuilderWithEnum(): 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);
}
}

/**
* @param class-string $cardClass
*
Expand Down

0 comments on commit 3fa2250

Please sign in to comment.