问题
I have this query
$qb->select('u')
->from('UserBundle:User', 'u')
->where('u.location = :identifier')
->orderBy('u.firstName', 'ASC')
->setParameter('identifier', 2);
I want that if $identifier is present then it should filter the results otherwise i get all the results like
$qb->select('u')
->from('UserBundle:User', 'u')
if($identifier)
->where('u.location = :identifier')
->orderBy('u.firstName', 'ASC')
if($identifier)
->setParameter('identifier', 2);
Is it possible
回答1:
It is possible, you just have to restructure your code.
$qb->select('u')
->from('UserBundle:User', 'u')
->orderBy('u.firstName', 'ASC');
if($identifier) {
$qb->where('u.location = :identifier')
->setParameter('identifier', 2);
}
来源:https://stackoverflow.com/questions/11965534/how-can-i-put-condition-in-doctrine-query-builder