具有左联接的对象属性的DQL访问id



我实现了这个sql,它可以毫无问题地工作

SELECT meeting.name, meeting.date, community.name, participation.isPresent, participation.user_id
FROM meeting
INNER JOIN community
ON meeting.community_id = community.id
AND community.is_active = 1
LEFT join participation
ON meeting.id = participation.meeting_id
AND participation.user_id = 1078
WHERE meeting.date >= CURRENT_DATE()
ORDER BY meeting.date DESC

我试图用条令查询生成器来复制它,但我从来没有得到正确的结果。用户id部分似乎不是leftJoin函数的一部分,而是全局应用于请求,这不是我想要的。

public function getNextMeetings()
{
$qb = $this->createQueryBuilder('m')
->select('m.name AS meeting, m.date, c.name AS community, p.isPresent', 'IDENTITY(p.user) AS user')
->innerJoin('m.community', 'c')
->where('c.isActive = true')
->leftJoin('m.participations', 'p')
//->leftJoin('p.user', 'u')
//->where('u.id = 1078 OR u.id IS NULL')
//->where('IDENTITY(p.user) = 1078')
->andWhere('m.date >= CURRENT_DATE()')
->orderBy('m.date', 'DESC');
return $qb->getQuery()->execute();
}

我的评论是我试图解决这个问题的。

检查使用QueryBuilder:高级API方法

更准确地说,定义odleftJoin((函数:

public function leftJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null);

您可以通过以下方式对加入的实体设置条件:

use DoctrineORMQueryExpr;
->leftJoin('m.participations', 'p', ExprJoin::WITH, 'p.user = :userId')
->setParameter('userId', 1078)

请注意,您不需要"meeting.id=participation.meeting_id"的条件,因为这是由m.participations关系自动应用于构造的联接的。

最新更新