3

我有一个查询:

$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_publishedandpublished_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 条件。如何做到这一点?

谢谢

4

2 回答 2

5

而不是你的最后两行

$q->andWhereIn('l.ldap_group_id', $permissions_id );
$q->orWhere('l.ldap_group_id IS NULL);

试试这个方法

$q->andWhere($qb->expr()->orx(
   $qb->expr()->in( 'l.ldap_group_id', $permissions_id ),
   $qb->expr()->isNull( 'l.ldap_group_id' );
于 2011-03-24T15:31:17.617 回答
3

好的解决方案在这里:

$q->andWhere('(l.ldap_group_id IN ( ' . implode(",", $permissions_id) . ') OR l.ldap_group_id IS NULL )' );
于 2011-03-24T16:03:21.910 回答