Search code examples
phpdoctrinedoctrine-query

Doctrine query builber: a problem adding multiple conditions with andWhere


I have a number of conditions:

if (!empty($requestData['year'])) {
                    $conditions[] = $qb->expr()->eq('dd.year', $requestData['year']);
}
if(!empty($requestData['month'])){
                    $conditions[] = $qb->expr()->eq('dd.month', $this->convertMonthStringToNumber($requestData['month']));
}

Which I am trying to add to the andWhere. I have found this solution:

$andX = $qb->expr()->andX();
$andX->addMultiple($conditions);
$qb->andWhere($andX);

But this throws me an error:

assert($lookahead !== null)
in [SERVER]\vendor\doctrine\orm\src\Query\Parser.php (line 2639) 

Solution

  • Try this:

    $andX = $qb->expr()->andX(...$conditions);
    $qb->andWhere($andX);