Symfony – Doctrine – Repository – WHERE value might be null or not

  doctrine, php, symfony, symfony4

Sometimes a category will have a value and sometimes it will be null (this condition is omitted).

The following will not return a product if it has a null category.

What’s the best way to resolve this without having to check if category is null and then use ->where('product.category is null')? I’m in a situation where I have 30 different fields where sometimes the value will be null and sometimes it won’t.

$this->createQueryBuilder('product')
    ->andWhere('product.category = :category')
    ->setParameter('category', $category)
    ->getQuery()
    ->getOneOrNullResult()
;

Source: Ask PHP

LEAVE A COMMENT