Ilari Kajaste
Ilari Kajaste

Reputation: 3291

Can Doctrine QueryBuilder andWhere() used without where()?

In Doctrine, when building a query with QueryBuilder, does the first whereclause 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

Answers (1)

peterjwest
peterjwest

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

Related Questions