Skip to content

Commit

Permalink
Fix enum change set recomputation on single entity
Browse files Browse the repository at this point in the history
  • Loading branch information
rmikalkenas committed Jun 28, 2023
1 parent dba90c1 commit 176dbc5
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/Doctrine/ORM/UnitOfWork.php
Expand Up @@ -1124,6 +1124,20 @@ public function recomputeSingleEntityChangeSet(ClassMetadata $class, $entity)
foreach ($actualData as $propName => $actualValue) {
$orgValue = $originalData[$propName] ?? null;

if (! empty($class->fieldMappings[$propName]['enumType'])) {
if (is_array($orgValue)) {
foreach ($orgValue as $id => $val) {
if ($val instanceof BackedEnum) {
$orgValue[$id] = $val->value;

Check warning on line 1131 in lib/Doctrine/ORM/UnitOfWork.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/UnitOfWork.php#L1129-L1131

Added lines #L1129 - L1131 were not covered by tests
}
}
} else {
if ($orgValue instanceof BackedEnum) {
$orgValue = $orgValue->value;
}
}
}

if ($orgValue !== $actualValue) {
$changeSet[$propName] = [$orgValue, $actualValue];
}
Expand Down
80 changes: 80 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/EnumTest.php
Expand Up @@ -260,6 +260,86 @@ 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->createQueryBuilder()
->from(Card::class, 'c')
->select('c')
->getQuery()
->getResult();

$this->_em->getUnitOfWork()->recomputeSingleEntityChangeSet(
$this->_em->getClassMetadata(Card::class),
$result[0]
);

self::assertFalse($this->_em->getUnitOfWork()->isScheduledForUpdate($result[0]));
}

public function testEnumArraySingeEntityChangeSets(): 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->createQueryBuilder()
->from(Scale::class, 's')
->select('s')
->getQuery()
->getResult();

$this->_em->getUnitOfWork()->recomputeSingleEntityChangeSet(
$this->_em->getClassMetadata(Card::class),
$result[0]
);

self::assertFalse($this->_em->getUnitOfWork()->isScheduledForUpdate($result[0]));
}

public function testEnumChangeSetsSimpleObjectHydrator(): void
{
$this->setUpEntitySchema([Card::class]);
Expand Down

0 comments on commit 176dbc5

Please sign in to comment.