学说查询:令人困惑的 addWhere、andWhere、orWhere
我有一个查询:
$q->andWhere($q->getRootAlias().'.is_published = ?', 1);
$q->andWhere($q->getRootAlias().'.published_at >= ?', time());
$q->leftJoin($q->getRootAlias().'.EventLdapGroup l');
$q->andWhereIn('l.ldap_group_id', $permissions_id );
$q->orWhere('l.ldap_group_id IS NULL);
输出:
FROM
Event r LEFT JOIN r.EventLdapGroup l
WHERE
r.is_published = ? AND
r.published_at >= ? AND
l.ldap_group_id IN (?, ?) OR
l.ldap_group_id IS NULL
唯一的问题是,如果ldap_group_id
为空(最后一个条件),它将删除is_published
和published_at
条件。
我想要类似的东西 [值要么在 ldap_group_id 中,要么为空]:
FROM
Event r LEFT JOIN r.EventLdapGroup l
WHERE
r.is_published = ? AND
r.published_at >= ? AND
(l.ldap_group_id IN (?, ?) OR l.ldap_group_id IS NULL)
我必须说我迷失了复杂的 where 条件。 如何实现这一目标?
谢谢
I have a query :
$q->andWhere($q->getRootAlias().'.is_published = ?', 1);
$q->andWhere($q->getRootAlias().'.published_at >= ?', time());
$q->leftJoin($q->getRootAlias().'.EventLdapGroup l');
$q->andWhereIn('l.ldap_group_id', $permissions_id );
$q->orWhere('l.ldap_group_id IS NULL);
which outputs :
FROM
Event r LEFT JOIN r.EventLdapGroup l
WHERE
r.is_published = ? AND
r.published_at >= ? AND
l.ldap_group_id IN (?, ?) OR
l.ldap_group_id IS NULL
The only problem is that if ldap_group_id
is null (the last condition) it'll remove the is_published
and published_at
conditions.
I want something like that [either the values are in ldap_group_id, or either it's null] :
FROM
Event r LEFT JOIN r.EventLdapGroup l
WHERE
r.is_published = ? AND
r.published_at >= ? AND
(l.ldap_group_id IN (?, ?) OR l.ldap_group_id IS NULL)
And i must say that i'm lost with the complex where condition.
How to achieve that ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
而不是你的最后两行
尝试这种方法
Instead of your last two lines
Try this approach
好的解决方案在这里:
Ok solution is here :