diff --git a/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php b/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php index 4470d3b0eea..5af8b876691 100644 --- a/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php +++ b/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php @@ -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; diff --git a/lib/Doctrine/ORM/Mapping/ReflectionEnumProperty.php b/lib/Doctrine/ORM/Mapping/ReflectionEnumProperty.php index 6482d50ea63..a162ebfd345 100644 --- a/lib/Doctrine/ORM/Mapping/ReflectionEnumProperty.php +++ b/lib/Doctrine/ORM/Mapping/ReflectionEnumProperty.php @@ -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 { @@ -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 { diff --git a/tests/Doctrine/Tests/ORM/Functional/EnumTest.php b/tests/Doctrine/Tests/ORM/Functional/EnumTest.php index afdf992d542..9e53b856087 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EnumTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EnumTest.php @@ -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 $foundCards */ + $foundCards = $this->_em->createQueryBuilder() + ->select('c') + ->from(Card::class, 'c') + ->where('c.suit = :suit') + ->setParameter('suit', Suit::Clubs) + ->getQuery() + ->getResult(); + + self::assertNotEmpty($foundCards); + foreach ($foundCards as $card) { + self::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]);