mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
can not set default value to enum case #6906
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 @jgroc-de on GitHub (Jan 13, 2022).
Bug Report
Summary
We dumbly tried to put a default value to a property set to a enum's case and
it does not work when generating the migration
which means we can not set default value based on enum case
(we can hardcode it but we are not happy with this solution)
Current behavior
given an enum:
trying
give us after a bin/console make:migration
trying
give us a migration like
which is the string "App\Enum\Suit::Hearts" and not the value of the enum case
How to reproduce
create an enum:
set a property in an entity linking to this enum
with a default value set to one of the enum case
make a migration
(in sf5):
bin/console make:migrationExpected behavior
trying
give us a migration like
@beberlei commented on GitHub (Jan 13, 2022):
@morozov I believe this could be best solved in DBAL
Schema\Columnby checking for aBackedEnumin the default value and resolving to $enum->value, what do you think?@morozov commented on GitHub (Jan 13, 2022):
Normally, the DBAL doesn't perform any explicit downcasting of types (e.g.
Stringable→__toString()). Since this is the ORM that owns the support for enums, it seems to make more sense if the ORM checked for aBackedEnumand replaced it with the value. I wouldn't expect aBackedEnumto be used in the DBAL without the ORM.@derrabus commented on GitHub (Jan 14, 2022):
I'm not sure if we should treat this as a bug. I realize that the enum column feature feels a bit incomplete without the ability to specify an actual enum case as default. But so far, the default was something we had passed directly to the schema manager and if we start to preprocess it, we should make sure we do it right. One idea (not sure how feasible this is) could be to reuse the query parameter preprocessing here which will support enums in 2.12.
The workaround for now would be to use the backed value directly. That may not be nice, but it'll give us time to implement enum cases as default values properly for 2.12.
@jgroc-de commented on GitHub (Jan 14, 2022):
As said, we dont like to put the default value directly.
So we had just drop the default value for now as we can live without it in our db, letting the entity setting the default value in the constructor.
And i agree, it's more like a "missing feature".
@michaeldnelson commented on GitHub (Jan 26, 2022):
Hmmm. I came here to report this feature not functioning in the orm but from this thread it seems like it might just be me.
results in:
in the object hydrator. Does anyone know if this case should function? Must I include an options default value? I prefer to manage my defaults in code.
@ThomasLandauer commented on GitHub (Jan 27, 2022):
@michaeldnelson The
enumTypeis working for me. The "Proxies" message sounds like you need to clear the cache ;-)@derrabus commented on GitHub (Jan 27, 2022):
@michaeldnelson Your problem has nothing to do with the feature that is discussed here. Please open a new issue if you think it's a bug or a discussion if you seek support.
@derrabus commented on GitHub (Mar 28, 2022):
#9616