diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index cc1c5ab22bc..a1b80c565b9 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -1124,6 +1124,20 @@ public function recomputeSingleEntityChangeSet(ClassMetadata $class, $entity) foreach ($actualData as $propName => $actualValue) { $orgValue = $originalData[$propName] ?? null; + if (isset($class->fieldMappings[$propName]['enumType'])) { + if (is_array($orgValue)) { + foreach ($orgValue as $id => $val) { + if ($val instanceof BackedEnum) { + $orgValue[$id] = $val->value; + } + } + } else { + if ($orgValue instanceof BackedEnum) { + $orgValue = $orgValue->value; + } + } + } + if ($orgValue !== $actualValue) { $changeSet[$propName] = [$orgValue, $actualValue]; } diff --git a/tests/Doctrine/Tests/ORM/Functional/EnumTest.php b/tests/Doctrine/Tests/ORM/Functional/EnumTest.php index 3da2ea5f4ff..1d817d8d2ed 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EnumTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EnumTest.php @@ -260,6 +260,78 @@ public function testEnumArrayInDtoHydration(): void self::assertEqualsCanonicalizing([Unit::Gram, Unit::Meter], $result[0]->supportedUnits); } + public function testEnumSingleEntityChangeSetsSimpleObjectHydrator(): void + { + $this->setUpEntitySchema([Card::class]); + + $card = new Card(); + $card->suit = Suit::Clubs; + + $this->_em->persist($card); + $this->_em->flush(); + $this->_em->clear(); + + $result = $this->_em->find(Card::class, $card->id); + + $this->_em->getUnitOfWork()->recomputeSingleEntityChangeSet( + $this->_em->getClassMetadata(Card::class), + $result + ); + + self::assertFalse($this->_em->getUnitOfWork()->isScheduledForUpdate($result)); + + $result->suit = Suit::Hearts; + + $this->_em->getUnitOfWork()->recomputeSingleEntityChangeSet( + $this->_em->getClassMetadata(Card::class), + $result + ); + + self::assertTrue($this->_em->getUnitOfWork()->isScheduledForUpdate($result)); + } + + public function testEnumSingleEntityChangeSetsObjectHydrator(): void + { + $this->setUpEntitySchema([Card::class]); + + $card = new Card(); + $card->suit = Suit::Clubs; + + $this->_em->persist($card); + $this->_em->flush(); + $this->_em->clear(); + + $result = $this->_em->find(Card::class, $card->id); + + $this->_em->getUnitOfWork()->recomputeSingleEntityChangeSet( + $this->_em->getClassMetadata(Card::class), + $result + ); + + self::assertFalse($this->_em->getUnitOfWork()->isScheduledForUpdate($result)); + } + + public function testEnumArraySingleEntityChangeSets(): void + { + $this->setUpEntitySchema([Scale::class]); + + $scale = new Scale(); + $scale->supportedUnits = [Unit::Gram]; + + $this->_em->persist($scale); + $this->_em->flush(); + $this->_em->clear(); + + $result = $this->_em->find(Scale::class, $scale->id); + + $this->_em->getUnitOfWork()->recomputeSingleEntityChangeSet( + $this->_em->getClassMetadata(Scale::class), + $result + ); + + self::assertFalse($this->_em->getUnitOfWork()->isScheduledForUpdate($result)); + } + public function testEnumChangeSetsSimpleObjectHydrator(): void { $this->setUpEntitySchema([Card::class]);