doctrine 2 - 查询构建器条件查询...如果语句? [英] doctrine 2 - query builder conditional queries... If statements?

查看:106
本文介绍了doctrine 2 - 查询构建器条件查询...如果语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的查询是doctirne 2.我在用户,私人或
public中有一个状态字段。我想要能够运行此查询并显示所有评论
其中status = public和private仅当userid =当前登录
用户标识(我知道,$ loggerUserVarID)

My query is doctirne 2. i have a status field in users, private or public. i want to be able to run this query and display all comments where status= public and private only if userid = current logged in user id(which i know, $loggerUserVarID)



  $q = $this->em->createQueryBuilder()
            ->select('c')
            ->from('\Entities\Comments', 'c')
            ->leftJoin('c.users', 'u')
            ->where('status = public')  ???  display all public comments but private if it belpongs to the logged in user.?
            ->setParameter(1, $loggerUserVarID)
            ->getQuery();

目前,我在使用if语句后,我得到了结果,有没有办法在此查询中执行if语句?

at the moment, i am using an if statement after i get thee results, is there a way to do an if statement inside this query?

推荐答案

所以,你想返回评论如果状态是public或ownerId是$ loggedUserVarID,对吗?

So, you want to return Comments "If status is 'public' or the ownerId is $loggedUserVarID", right?

请注意,如果$ loggedUserVarID与所有者相匹配,则不太关心状态。

Note that if $loggedUserVarID matches the owner, you don't really care about status.

查看querybuilder和dql文档。他们很清楚地解释了如何将条件复合在一起。

Check out the querybuilder and dql docs. They explain pretty clearly how to put together complex where conditions.

你可能想要的是:

$q = $this->em->createQueryBuilder()
            ->select('c')
            ->from('\Entities\Comments', 'c')
            ->join('c.users', 'u')
            ->where(
                $qb->expr()->orX(
                    $qb->expr()->eq('status','public'),
                    $qb->expr()->eq('u.id',$loggedInUser)
                )
           )
         ->setParameter(1, $loggerUserVarID)
         ->getQuery();

这篇关于doctrine 2 - 查询构建器条件查询...如果语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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