Does Doctrine repository's findBy method support Enums?

Viewed 43

On a Symfony 6.0.8 project using Doctrine 2.7.0, I've added some Enums as field types for my entities (I followed this tutorial: https://smaine-milianni.medium.com/use-php-enums-as-doctrine-type-in-symfony-85909aa0a19a) and everything works fine.

What bugs me is that in my repositories, I can filter by these fields with this

$qb =  $this->createQueryBuilder('e');
$query = $qb->select("e")
    ->where('e.status = :status')
    ->setParameter('status', Status::STATUS_ACCEPTED)
    ;

return $query->getQuery()->getResult();

but not this

$criterias = array(
    "status" => Status::STATUS_ACCEPTED,
);
$repository->findBy($criterias);

Adding ->value didn't help either. As soon as I try to filter with Enums, it returns an empty set.

Is there something I'm missing?

1 Answers

setParameter works because of this code:

// \Doctrine\ORM\AbstractQuery::processParameterValue
if ($value instanceof BackedEnum) {
    return $value->value;
}

I can't be sure about why criteria doesn't work in that way, but I think that's because of \Doctrine\ORM\Persisters\Entity\BasicEntityPersister::expandParameters (here). But weird thing that if you would try to pass ->value that would be int|string, and it should work fine. Please check that ->value returns a string, that contains in database column.

Related