Reputation: 3291
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);
}
Upvotes: 3
Views: 1780
Reputation: 4452
Yes that will work. where()
deletes any other criteria before adding those submitted, while andWhere()
appends to an AND
expression with any existing criteria.
Upvotes: 7