Skip to content

Add type inferral for backed enums in Parameter as part of QueryBuilder #9372

@tomudding

Description

@tomudding

Feature Request

Q A
New Feature yes
RFC no
BC Break no

Summary

With the release of v2.11.0 just a few hours ago, support for PHP 8.1's enums was added. This works great, however, I would like to see that the setParameter() function (or rather the Parameter which is created in that function) has support for automatically inferring the type of backed enums.

Here an example:

enum Suit: int {
    case Hearts = 0;
    case Diamonds = 1; 
    case Clubs = 2;
    case Spades = 3;
}

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

    #[Column(type: "integer", enumType: Suit::class)]
    private Suit $suit;

Then at some point you could want to do something along the of:

$qb = $em->getRepository(Card::class)->createQueryBuilder('c');
$qb->where('c.suit = :suit')
    ->setParameter('suit', Suit::Hearts); // instead of Suit::Hearts->value

$qb->getQuery->getResult();

However, now you get the following error:

Error: Object of class Suit could not be converted to string.

Where I would have expected it to say integer. string is given because it is the default type (\Doctrine\DBAL\ParameterType::STRING) in \Doctrine\ORM\Query\ParameterTypeInferer::inferType. Which has no knowledge of what a backed enum is.

Clearly, this is also shows that DBAL is incomplete, as it has no idea what to do with the enum (which type of backed enum does not matter, you could force \Doctrine\DBAL\Types\Types::INTEGER in setParameter and DBAL would complain about being unable to convert the enum into an integer). However, that is an separate issue and not what this feature request is about (but it does depend on each other).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions