Yes that will work. where()
deletes any other criteria before adding those submitted, while andWhere()
appends to an AND
expression with any existing criteria.
Can Doctrine QueryBuilder andWhere() used without where()?
-
11-04-2022 - |
Question
In Doctrine, when building a query with QueryBuilder, does the first where
clause have to be defined with $qb->where()
or can I use $qb->andWhere()
directly. For example, is this valid:
$qb->select('Mystuff\Entity\User','u');
$qb->andWhere('usertype = :usertype');
$qb->andWhere('usercategory = :usercategory');
Or, as a more relevant example:
$filter = array('usertype'=>'basic','usercategory'=>'business');
$qb->select('Mystuff\Entity\User','u');
foreach ($filter as $fkey => $fval) {
$qb->andWhere($fkey.' = :'.$fval);
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow