学说 2 - 查询生成器条件查询... If 语句?
我的查询是doctirne 2。我在用户中有一个状态字段,私人或 民众。我希望能够运行此查询并显示所有评论 仅当 userid = 当前登录时,状态 = 公共和私有 用户 ID(我知道,$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语句?
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();
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么,您想要返回注释“If status is 'public' or theownerId is $loggedUserVarID”,对吗?
请注意,如果 $loggedUserVarID 与所有者匹配,则您并不真正关心状态。
查看 querybuilder 和 dql 文档。他们非常清楚地解释了如何组合复杂的 where 条件。
您可能想要的是这样的:
So, you want to return Comments "If status is 'public' or the ownerId is $loggedUserVarID", right?
Note that if $loggedUserVarID matches the owner, you don't really care about status.
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: