mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Array to string conversion when invalid value in enum array column #7463
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @maximecolin on GitHub (Jan 21, 2025).
Bug Report
Summary
When querying an entity having an array of enum containing a value that does not exists in the PHP enum, I got the following error:
Current behavior
Using a json column with enumType, if the database contains a value that is not in the enum, so an exception is thrown by
AbstractHydrator::buildEnumthen converted to another exception in SimpleObjectHydrator. During this conversion, the original value is casted into string. In case of array of enum (json or simple array),$originalValuecontains an array of string that can not be casted to string. So we got the "Array to string conversion" error instead of a proper error about a case not listed in the enum.This case is pretty common, for exemple when a case have been removed from the PHP enum, or when a value that have been added on a branch, when switching to another branch, the value is still in the database but does not exist anymore on the PHP enum.
Expected behavior
A proper exception explaining a case in the database is not listed in the enum like for single enum case columns.
How to reproduce
I also created a test that reproduce this error : https://github.com/doctrine/orm/pull/11795
@greg0ire commented on GitHub (Jan 21, 2025):
This was introduced in
9d5ab4ce76@HypeMC , can you please take a look at this?
@HypeMC commented on GitHub (Jan 22, 2025):
@greg0ire
This seems to be a 3.x exclusive bug, it works fine on 2.x, see #11799. The problem is that 2.x uses the ORMReflectionEnumProperty, which handles invalid values:5a599233c9/src/Mapping/ReflectionEnumProperty.php (L54-L86)while 3.x usesEnumReflectionPropertyfrom the persistence repo, which doesn't:133bb28255/src/Persistence/Reflection/EnumReflectionProperty.php (L106-L122)I'm guessing the solution would be to handle this inEnumReflectionProperty. If that's the case, I'll gladly open a PR.Also, it'd probably be good to merge #11799 to prevent any future bugs in 2.x.Scratch that, I was wrong. The reproducer wasn't complete, it was missing
$rsm->addEnumResult('s__supported_units', Unit::class);. With that line of code, I get the error on 2.x as well. See #11799 for possible fix.