如何在条件查询生成器中放入条件 [英] How can i put condition in doctrine query builder

查看:134
本文介绍了如何在条件查询生成器中放入条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此查询

$qb->select('u')
           ->from('UserBundle:User', 'u')
           ->where('u.location = :identifier')
           ->orderBy('u.firstName', 'ASC')
           ->setParameter('identifier', 2);

我希望如果$ identifier存在,则应该过滤结果,否则我得到所有类似的结果

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);

有可能

推荐答案

可能的是,您只需要重组代码即可。

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);
}

这篇关于如何在条件查询生成器中放入条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆