From 1ed0057276b3846ba6f1c3e77f926bb69467a033 Mon Sep 17 00:00:00 2001 From: HypeMC <2445045+HypeMC@users.noreply.github.com> Date: Mon, 10 Oct 2022 11:59:04 +0200 Subject: [PATCH] Fix change set computation with enums (#10074) * add failing testcase test enum Change sets * Fix change set computation with enums Co-authored-by: Olda Salek --- lib/Doctrine/ORM/UnitOfWork.php | 5 +++ .../Tests/ORM/Functional/EnumTest.php | 40 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 3a157c404b8..a3f1794899b 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -4,6 +4,7 @@ namespace Doctrine\ORM; +use BackedEnum; use DateTimeInterface; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; @@ -743,6 +744,10 @@ public function computeChangeSet(ClassMetadata $class, $entity) $orgValue = $originalData[$propName]; + if ($orgValue instanceof BackedEnum) { + $orgValue = $orgValue->value; + } + // skip if value haven't changed if ($orgValue === $actualValue) { continue; diff --git a/tests/Doctrine/Tests/ORM/Functional/EnumTest.php b/tests/Doctrine/Tests/ORM/Functional/EnumTest.php index 9e53b856087..151067ef280 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EnumTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EnumTest.php @@ -260,6 +260,46 @@ public function testEnumArrayInDtoHydration(): void self::assertEqualsCanonicalizing([Unit::Gram, Unit::Meter], $result[0]->supportedUnits); } + public function testEnumChangeSetsSimpleObjectHydrator(): 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()->computeChangeSets(); + + self::assertFalse($this->_em->getUnitOfWork()->isScheduledForUpdate($result)); + } + + public function testEnumChangeSetsObjectHydrator(): 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->createQueryBuilder() + ->from(Card::class, 'c') + ->select('c') + ->getQuery() + ->getResult(); + + $this->_em->getUnitOfWork()->computeChangeSets(); + + self::assertFalse($this->_em->getUnitOfWork()->isScheduledForUpdate($result[0])); + } + public function testFindByEnum(): void { $this->setUpEntitySchema([Card::class]);