Skip to content

Commit 0b9060c

Browse files
authored
Merge pull request #10806 from rmikalkenas/fix-enum-recomputation
2 parents 514f6b8 + c1018fe commit 0b9060c

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

lib/Doctrine/ORM/UnitOfWork.php

+14
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,20 @@ public function recomputeSingleEntityChangeSet(ClassMetadata $class, $entity)
11241124
foreach ($actualData as $propName => $actualValue) {
11251125
$orgValue = $originalData[$propName] ?? null;
11261126

1127+
if (isset($class->fieldMappings[$propName]['enumType'])) {
1128+
if (is_array($orgValue)) {
1129+
foreach ($orgValue as $id => $val) {
1130+
if ($val instanceof BackedEnum) {
1131+
$orgValue[$id] = $val->value;
1132+
}
1133+
}
1134+
} else {
1135+
if ($orgValue instanceof BackedEnum) {
1136+
$orgValue = $orgValue->value;
1137+
}
1138+
}
1139+
}
1140+
11271141
if ($orgValue !== $actualValue) {
11281142
$changeSet[$propName] = [$orgValue, $actualValue];
11291143
}

tests/Doctrine/Tests/ORM/Functional/EnumTest.php

+72
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,78 @@ public function testEnumArrayInDtoHydration(): void
260260
self::assertEqualsCanonicalizing([Unit::Gram, Unit::Meter], $result[0]->supportedUnits);
261261
}
262262

263+
public function testEnumSingleEntityChangeSetsSimpleObjectHydrator(): void
264+
{
265+
$this->setUpEntitySchema([Card::class]);
266+
267+
$card = new Card();
268+
$card->suit = Suit::Clubs;
269+
270+
$this->_em->persist($card);
271+
$this->_em->flush();
272+
$this->_em->clear();
273+
274+
$result = $this->_em->find(Card::class, $card->id);
275+
276+
$this->_em->getUnitOfWork()->recomputeSingleEntityChangeSet(
277+
$this->_em->getClassMetadata(Card::class),
278+
$result
279+
);
280+
281+
self::assertFalse($this->_em->getUnitOfWork()->isScheduledForUpdate($result));
282+
283+
$result->suit = Suit::Hearts;
284+
285+
$this->_em->getUnitOfWork()->recomputeSingleEntityChangeSet(
286+
$this->_em->getClassMetadata(Card::class),
287+
$result
288+
);
289+
290+
self::assertTrue($this->_em->getUnitOfWork()->isScheduledForUpdate($result));
291+
}
292+
293+
public function testEnumSingleEntityChangeSetsObjectHydrator(): void
294+
{
295+
$this->setUpEntitySchema([Card::class]);
296+
297+
$card = new Card();
298+
$card->suit = Suit::Clubs;
299+
300+
$this->_em->persist($card);
301+
$this->_em->flush();
302+
$this->_em->clear();
303+
304+
$result = $this->_em->find(Card::class, $card->id);
305+
306+
$this->_em->getUnitOfWork()->recomputeSingleEntityChangeSet(
307+
$this->_em->getClassMetadata(Card::class),
308+
$result
309+
);
310+
311+
self::assertFalse($this->_em->getUnitOfWork()->isScheduledForUpdate($result));
312+
}
313+
314+
public function testEnumArraySingleEntityChangeSets(): void
315+
{
316+
$this->setUpEntitySchema([Scale::class]);
317+
318+
$scale = new Scale();
319+
$scale->supportedUnits = [Unit::Gram];
320+
321+
$this->_em->persist($scale);
322+
$this->_em->flush();
323+
$this->_em->clear();
324+
325+
$result = $this->_em->find(Scale::class, $scale->id);
326+
327+
$this->_em->getUnitOfWork()->recomputeSingleEntityChangeSet(
328+
$this->_em->getClassMetadata(Scale::class),
329+
$result
330+
);
331+
332+
self::assertFalse($this->_em->getUnitOfWork()->isScheduledForUpdate($result));
333+
}
334+
263335
public function testEnumChangeSetsSimpleObjectHydrator(): void
264336
{
265337
$this->setUpEntitySchema([Card::class]);

0 commit comments

Comments
 (0)