【问题标题】:DQL access id from object property with left join来自具有左连接的对象属性的 DQL 访问 ID
【发布时间】:2018-11-27 14:52:48
【问题描述】:

我意识到这个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();
}

我的 cmets 是我试图解决此问题的工具。

【问题讨论】:

    标签: symfony doctrine-orm doctrine-query


    【解决方案1】:

    查看Working with QueryBuilder: High level API methods

    更准确地说,leftJoin()函数的定义:

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

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

    use Doctrine\ORM\Query\Expr;
    
    ->leftJoin('m.participations', 'p', Expr\Join::WITH, 'p.user = :userId')
    ->setParameter('userId', 1078)
    

    请注意,“meeting.id = entry.meeting_id”不需要条件,因为这是由关系 m.participations 自动应用到构造的连接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-29
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多