[PR #9304] Add support for PHP 8.1 enums. #11505

Open
opened 2026-01-22 16:10:59 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/9304

State: closed
Merged: Yes


ORM based support for enums.

enum Suit: string {
    case Hearts = 'H';
    case Diamonds = 'D'; 
    case Clubs = 'C';
    case Spades = 'S';
}

#[Entity]
class Card
{
    /** ... */

    #[Column(type: 'string', enumType: Suit::class)]
    public $suit;

It works based on a new ReflectionEnumProperty that wraps around other reflection property types. This avoids the problem of other approaches that hook into DBAL Type abstraction, since that would require some kind of paramterization which is not supported at the moment. In addition it would at the moment require to register each enum explicitly, which this approach does not.

This includes support for typed property detection, the previous example could be written as:

#[Column]
public Suit $suit;

As for error handling, when the database returns an invalid enum case, then the regular ValueError is not caught that BackedEnum::from throws.

**Original Pull Request:** https://github.com/doctrine/orm/pull/9304 **State:** closed **Merged:** Yes --- ORM based support for enums. ```php enum Suit: string { case Hearts = 'H'; case Diamonds = 'D'; case Clubs = 'C'; case Spades = 'S'; } #[Entity] class Card { /** ... */ #[Column(type: 'string', enumType: Suit::class)] public $suit; ``` It works based on a new `ReflectionEnumProperty` that wraps around other reflection property types. This avoids the problem of other approaches that hook into DBAL Type abstraction, since that would require some kind of paramterization which is not supported at the moment. In addition it would at the moment require to register each enum explicitly, which this approach does not. This includes support for typed property detection, the previous example could be written as: ```php #[Column] public Suit $suit; ``` As for error handling, when the database returns an invalid enum case, then the regular `ValueError` is not caught that `BackedEnum::from` throws.
admin added the pull-request label 2026-01-22 16:10:59 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#11505