mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Alternative DiscriminatorMap declaration #6961
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 @michnovka on GitHub (Apr 18, 2022).
Feature Request
I have a custom DBAL type and I wish to use it as a discriminator column for JOINED inheritance type
Summary
In my case I use custom type that works with enums. But any custom type using any non-scalar PHP value would cause the same issues:
The issue is that while any type can be used for
DiscriminatorColumn, theDiscriminatorMapdefinition accepts only format likeThe
VALUE_OF_COLUMNis the actual PHP value used by theTypethats converted to DB value withType::convertToDatabaseValue().And here lies the problem
in PHP as of 8.1 we cannot use objects as array keys. Therefore no objects / enums can be used inside
DiscriminatorMapsolely for this limitation. TheJoinedSubclassPersisterhandles the conversion through DBAL Type correctly, but the PHP limitation makes it impossible to use any non-scalar keys.Proposed solution
I propose an alternative declaration of
DiscriminatorMap, but keep the old one for simplicity and BC.So my code would look:
This would allow any custom type to be used as discriminator
@derrabus commented on GitHub (Apr 19, 2022):
Sounds reasonable, especially if we want to support backed enumerations as discriminators.
@michnovka commented on GitHub (Apr 19, 2022):
This will require redesign of internal
ClassMetadataInfo::$discriminatorMapas right now it also uses same structure with array key being the actual discriminator. Thats a public property, so there is a BC break. Otherwise wed have to copy lot of code to handle this special case, to keep BC.@michnovka commented on GitHub (Apr 19, 2022):
@beberlei would like your input on this too, please.
@michnovka commented on GitHub (Apr 19, 2022):
How about we call the
getDatabaseValuewhen constructing the discriminator map? And then when it is inserted into SQL, we no longer need to do that. This would allow us to keep the sameClassMetadataInfo::$discriminatorMap. I dont think BC would be broken, as currently it cannot work for any non-scalar types, and those have the same PHP value as DB value@Gabb1995 commented on GitHub (Jan 20, 2023):
is it at all possible to have php 8.1 enums as discriminator maps? I tried using the ->value method but it gives Constant expiression contains invalid operations.
@michnovka commented on GitHub (Jan 20, 2023):
@Gabb1995 this question does not belong here, but since you already asked, the answer is no. Using
Enum::enumCase->valueinside attributes is a PHP 8.2 feature.@whataboutpereira commented on GitHub (Jan 18, 2025):
I see DiscriminatorColumn takes enumType and I tried to use an enum there.
The entities actually work, but generating migration fails with:
This yields Doctrine\DBAL\Platforms\MySQL80Platform requires the values of a ENUM column to be specified.Is this still unsupported or am I missing something else?
Ideally it would be nice to return the whole discriminator map from the enum.