学说 2 - 查询生成器条件查询...如果语句? [英] doctrine 2 - query builder conditional queries... If statements?

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

问题描述

我的查询是doctirne 2.我在用户中有一个状态字段,私人或上市.我希望能够运行此查询并显示所有评论其中 status= public 和 private 仅当 userid = 当前登录时用户 ID(我知道,$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('EntitiesComments', '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?

推荐答案

那么,你想返回 Comments "If status is 'public' or the ownerId is $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 文档.他们非常清楚地解释了如何组合复杂的 where 条件.

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

你可能想要的是这样的:

What you probably want is something like:

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

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

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