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 id hash of entity with enum as identifier #10508

Merged
merged 1 commit into from
May 11, 2023

Conversation

Gwemox
Copy link
Contributor

@Gwemox Gwemox commented Feb 8, 2023

Current situation
When an entity have a backed enum as identifier, Unit Of Work tries to cast to string when generating the hash of the id.

Error "Object of class MyEnum could not be converted to string"

Suggested fix
This PR fixes the problem by adding function to get id of entity with backed enum check.

Tests added
It seems tests for this feature were completely missing, so I added them.

Fixes #10471
Fixes #10334

@Gwemox Gwemox force-pushed the fix-enum-identity branch 11 times, most recently from c7ddd5b to 63afbeb Compare February 11, 2023 12:58
@Gwemox
Copy link
Contributor Author

Gwemox commented Feb 14, 2023

Can someone validate the workflow?

@derrabus
Copy link
Member

Right away, sorry for the delay. 😓

@Gwemox
Copy link
Contributor Author

Gwemox commented Feb 15, 2023

Thank's @derrabus !
I fixed a problem

@Gwemox
Copy link
Contributor Author

Gwemox commented Mar 6, 2023

@derrabus Does this seem correct to you?

@Gwemox Gwemox marked this pull request as draft March 23, 2023 16:19
@Gwemox Gwemox marked this pull request as ready for review March 23, 2023 16:19
@Gwemox
Copy link
Contributor Author

Gwemox commented Apr 3, 2023

@greg0ire can you review this ? Do you expect anything from me?

/**
* Gets the id hash of an entity by its identifier.
*
* @param array<mixed, mixed> $identifier The identifier of an entity
Copy link
Member

Choose a reason for hiding this comment

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

What's the key type for this array? It should be at least array-key, but maybe you can narrow it down even more. Maybe this is even a list<mixed>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PHPStan tells me it's called with an int or string key.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, then use array-key I guess.

lib/Doctrine/ORM/UnitOfWork.php Show resolved Hide resolved
*
* @return string The entity id hash.
*/
public static function getIdHashByIdentifer($identifier)
Copy link
Member

Choose a reason for hiding this comment

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

Let's make this method final please.

Copy link
Member

Choose a reason for hiding this comment

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

Also, it should be "Identifier" with an i


/**
* @OneToMany(targetEntity="GH10334Foo", mappedBy="collection", cascade={"persist", "remove"})
* @var Collection $foos
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @var Collection $foos
* @var Collection<GH10334Foo> $foos

return $this->name;
}

public function setName(string $name): void
Copy link
Member

Choose a reason for hiding this comment

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

Please remove any unused getter/setter

When an entity have a backed enum as identifier, `UnitOfWork` tries to
cast to string when generating the hash of the id.
This fix calls `->value` when identifier is a `BackedEnum`.
Fixes doctrine#10471
Fixes doctrine#10334
@Gwemox
Copy link
Contributor Author

Gwemox commented Apr 3, 2023

Thank you for this quick review, I made the requested changes.

@Gwemox Gwemox force-pushed the fix-enum-identity branch from c4df297 to 09b4a75 Compare April 3, 2023 15:21
greg0ire
greg0ire previously approved these changes Apr 3, 2023
@BartVanGestel
Copy link

@Gwemox @greg0ire could you merge this PR, we currently have this exact problem and this will fix alot of unneeded persists.

@Gwemox
Copy link
Contributor Author

Gwemox commented May 11, 2023

@Gwemox @greg0ire could you merge this PR, we currently have this exact problem and this will fix alot of unneeded persists.

I don't have merge rights

@greg0ire greg0ire changed the base branch from 2.14.x to 2.15.x May 11, 2023 12:01
@greg0ire greg0ire dismissed their stale review May 11, 2023 12:01

The base branch was changed.

@greg0ire
Copy link
Member

Ping @SenseException @derrabus

@greg0ire greg0ire merged commit 52c3d9d into doctrine:2.15.x May 11, 2023
@greg0ire greg0ire added this to the 2.15.2 milestone May 11, 2023
@greg0ire greg0ire added the Bug label May 11, 2023
@wmouwen
Copy link

wmouwen commented May 31, 2023

@greg0ire Could you please tag a release containing this bugfix? It is cause for major locking issues on our systems and blocking releases of our software.

2023-05-31-090211_771x294_scrot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
6 participants