Skip to content

Commit 176dbc5

Browse files
committed
Fix enum change set recomputation on single entity
1 parent dba90c1 commit 176dbc5

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-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 (! empty($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

+80
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,86 @@ 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->createQueryBuilder()
305+
->from(Card::class, 'c')
306+
->select('c')
307+
->getQuery()
308+
->getResult();
309+
310+
$this->_em->getUnitOfWork()->recomputeSingleEntityChangeSet(
311+
$this->_em->getClassMetadata(Card::class),
312+
$result[0]
313+
);
314+
315+
self::assertFalse($this->_em->getUnitOfWork()->isScheduledForUpdate($result[0]));
316+
}
317+
318+
public function testEnumArraySingeEntityChangeSets(): void
319+
{
320+
$this->setUpEntitySchema([Scale::class]);
321+
322+
$scale = new Scale();
323+
$scale->supportedUnits = [Unit::Gram];
324+
325+
$this->_em->persist($scale);
326+
$this->_em->flush();
327+
$this->_em->clear();
328+
329+
$result = $this->_em->createQueryBuilder()
330+
->from(Scale::class, 's')
331+
->select('s')
332+
->getQuery()
333+
->getResult();
334+
335+
$this->_em->getUnitOfWork()->recomputeSingleEntityChangeSet(
336+
$this->_em->getClassMetadata(Card::class),
337+
$result[0]
338+
);
339+
340+
self::assertFalse($this->_em->getUnitOfWork()->isScheduledForUpdate($result[0]));
341+
}
342+
263343
public function testEnumChangeSetsSimpleObjectHydrator(): void
264344
{
265345
$this->setUpEntitySchema([Card::class]);

0 commit comments

Comments
 (0)