Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix enum change set recomputation on single entity #10806

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
72 changes: 72 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down