来自具有左联接的对象属性的DQL访问ID

时间:2018-11-27 14:52:49

标签: symfony doctrine-orm doctrine-query

我意识到这个sql可以正常工作

CREATE TRIGGER test
ON [dbo].[table]
AFTER INSERT
AS 
BEGIN
    DECLARE @client_id INT;
    DECLARE @id INT;

    SELECT @client_id = client_id 
    FROM inserted;

    SELECT @id = ISNULL(MAX(clifor_id), 0) + 1 
    FROM table
    WHERE client_id = @client_id

    UPDATE [dbo].[table]
    SET [id] = @id
    WHERE ?????
END

我正在尝试使用教义查询生成器来重现它,但是我从未得到正确的结果。用户ID部分似乎不是leftJoin函数的一部分,而是全局应用于请求,这不是我想要的。

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

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

1 个答案:

答案 0 :(得分: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 =参与.meeting_id”的条件,因为这是通过与关联的 m.participations 关系自动应用的。