Array to string conversion when invalid value in enum array column #7463

Closed
opened 2026-01-22 15:52:01 +01:00 by admin · 2 comments
Owner

Originally created by @maximecolin on GitHub (Jan 21, 2025).

Bug Report

Q A
Version 3.3.1
Previous Version if the bug is a regression not sure, it worked some time ago in 2.x

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:

Uncaught PHP Exception ErrorException: "Warning: Array to string conversion" at SimpleObjectHydrator.php line 148 {"exception":"[object] (ErrorException(code: 0): Warning: Array to string conversion at /srv/app/back/vendor/doctrine/orm/src/Internal/Hydration/SimpleObjectHydrator.php:148)"}

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::buildEnum then 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), $originalValue contains 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

  1. Create an entity with an array of enum (example bellow)
  2. Add in database an array containing a value not the enum cases
  3. Fetch the entity

I also created a test that reproduce this error : https://github.com/doctrine/orm/pull/11795

Originally created by @maximecolin on GitHub (Jan 21, 2025). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |-------------------------------------------- | ------ | Version | 3.3.1 | Previous Version if the bug is a regression | not sure, it worked some time ago in 2.x #### 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: > Uncaught PHP Exception ErrorException: "Warning: Array to string conversion" at SimpleObjectHydrator.php line 148 {"exception":"[object] (ErrorException(code: 0): Warning: Array to string conversion at /srv/app/back/vendor/doctrine/orm/src/Internal/Hydration/SimpleObjectHydrator.php:148)"} #### 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::buildEnum` then converted to another exception in SimpleObjectHydrator. During this conversion, [the original value is casted into string](https://github.com/doctrine/orm/blob/3.3.x/src/Internal/Hydration/SimpleObjectHydrator.php#L148). In case of array of enum (json or simple array), `$originalValue` contains 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 1. Create an entity with an array of enum (example bellow) 2. Add in database an array containing a value not the enum cases 3. Fetch the entity I also created a test that reproduce this error : https://github.com/doctrine/orm/pull/11795
admin added the Bug label 2026-01-22 15:52:01 +01:00
admin closed this issue 2026-01-22 15:52:01 +01:00
Author
Owner

@greg0ire commented on GitHub (Jan 21, 2025):

This was introduced in 9d5ab4ce76

@HypeMC , can you please take a look at this?

@greg0ire commented on GitHub (Jan 21, 2025): This was introduced in 9d5ab4ce763810f42d2079bf42b8bf48264ffa5c @HypeMC , can you please take a look at this?
Author
Owner

@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 ORM ReflectionEnumProperty, which handles invalid values:
5a599233c9/src/Mapping/ReflectionEnumProperty.php (L54-L86)

while 3.x uses EnumReflectionProperty from the persistence repo, which doesn't:

133bb28255/src/Persistence/Reflection/EnumReflectionProperty.php (L106-L122)

I'm guessing the solution would be to handle this in EnumReflectionProperty. 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.

@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 ORM `ReflectionEnumProperty`, which handles invalid values:~~ ~~https://github.com/doctrine/orm/blob/5a599233c99dea00df75be381da30311e055a657/src/Mapping/ReflectionEnumProperty.php#L54-L86~~ ~~while 3.x uses `EnumReflectionProperty` from the persistence repo, which doesn't:~~ ~~https://github.com/doctrine/persistence/blob/133bb2825572ee59e506c0bb7d6c9658506619a4/src/Persistence/Reflection/EnumReflectionProperty.php#L106-L122~~ ~~I'm guessing the solution would be to handle this in `EnumReflectionProperty`. 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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7463