Elao/PhpEnums

[2.X] Doctrine AbstractEnumType

Closed this issue · 3 comments

Hello,

I use your bundle in a project with symfony6.1 / doctrine 2.13.3.

Doctrine cast automatically BackEndEnum to their value :
https://github.com/doctrine/orm/blob/06c77cebb5aa7ee3cb57f8cc0d2224538de154d7/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php#L2002

In your AbstractEnumType method convertToPhpValue check if is a BackEnum but doctrine cast before to Enum type :

throw new InvalidArgumentException(sprintf(

I have to override generated Type to avoid this method.

Step to reproduce :
Doing a findBy with Enum criteria :

       $this->eventRepository->findBy([
            'eventType' => EventType::SESSION
        ]);

Have you any clean solution for me ?

Thanks

Hi @nicolastiran

It seems it's a BC break in Doctrine introduced in doctrine/orm#9453 when using custom DBAL types. Would you mind open an issue about it on the Doctrine repository?

@nicolastiran is this still an issue with doctrine orm 2.13.4? We introduced multiple enum-related changes and fixes, versions 2.13.2 and 2.13.3 had some enum related BC breaks.

If your issue persists please provide an example code of the issue.

It happens because BasicEntityPersister calls getValues() function which converts enums to strings. However it still internally tracks the type of the field as the AbstractEnumType, so it does eventually call convertToDatabaseValue.

Possible fix idea (needs testing):

        if ($value !== null && !($value instanceof BackedEnum)) {
            /** @var DBALSQLEnumTypeInterface $class */
            $class = static::getEnumClass();
            $value = $class::tryFrom($value);
        }