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 change set computation with enums #10074

Merged
merged 2 commits into from Oct 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/Doctrine/ORM/UnitOfWork.php
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ORM;

use BackedEnum;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
Expand Down Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is broken, and I'm pretty sure this is why. Based on the changes in line 747, we probably also need to have:

if ($actualValue instanceof BackedEnum) {
    $actualValue = $actualValue->value;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don think so. Check the code a bit above:

https://github.com/HypeMC/orm/blob/a1ed32f697f66d33de42fe96b1ea90e873ba2d5f/lib/Doctrine/ORM/UnitOfWork.php#L670

then

https://github.com/HypeMC/orm/blob/a1ed32f697f66d33de42fe96b1ea90e873ba2d5f/lib/Doctrine/ORM/UnitOfWork.php#L699

So actual value is fetched through reflection. And if you check the ReflectionEnumProperty::getValue()

https://github.com/HypeMC/orm/blob/a1ed32f697f66d33de42fe96b1ea90e873ba2d5f/lib/Doctrine/ORM/Mapping/ReflectionEnumProperty.php#L36-L63

It returns the $enum->value already. Therefore your proposed

if ($actualValue instanceof BackedEnum) {
    $actualValue = $actualValue->value;
}

seems redundant.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be wrong. All I know is that this patch broke several of our entities that use enums, and that seemed like a possibility. I haven't tested the theory.

continue;
Expand Down
40 changes: 40 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/EnumTest.php
Expand Up @@ -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]);
Expand Down