【发布时间】:2015-05-19 09:19:37
【问题描述】:
我的问题与此类似,但有所不同。 Can you join on a subquery with Doctrine 2 DQL?
无论如何,我都想获得所有的rooms,然后加入属于在给定日期存在的booking 的任何occupants。
例如(一个普通的 mysql 结果集 - 教义会返回对象):
Room ID | Occupant ID | Booking ID | Booking Start | Booking End
1 | 1 | 1 | Before today | After today
1 | 2 | 1 | Before today | After today
2 | NULL | NULL | NULL | NULL
这是我正在尝试的:
SELECT r, a, b
FROM MyBundle:Room r
LEFT JOIN r.occupants a
WITH a.booking is not null
LEFT JOIN a.booking b
WITH b.enrolmentStart <= :date
AND b.enrolmentEnd >= :date
AND b.status = 1
ORDER BY r.number ASC
不幸的是,这会获取所有房间,以及曾经住过那个房间的所有人,但只有在给定日期存在的预订。
另一方面,如果我更改为以下内容, 我只得到在给定日期有预订的房间。
LEFT JOIN r.occupants a
WITH a.booking is not null
JOIN a.booking b
如果我尝试以下操作,Doctrine 说它没想到在 'a' 之后有一个点。
LEFT JOIN r.occupants a
WITH a.booking.enrolmentStart <= :date
AND a.booking.enrolmentEnd >= :date
AND a.booking.status = 1
LEFT JOIN a.booking b
最后,如果我尝试以下操作,教义对订单不满意。
LEFT JOIN r.occupants a
WITH b.enrolmentStart <= :date
AND b.enrolmentEnd >= :date
AND b.status = 1
LEFT JOIN a.booking b
有什么想法吗?
【问题讨论】:
标签: symfony join doctrine-orm dql with-statement