我正在使用使用Doctrine 1.2的遗留系统。我正在尝试创建一个具有多个嵌套条件的查询,如下所示:
SELECT DISTINCT u.*
FROM Users AS u
INNER JOIN Workplaces AS w ON u.workplaceId = w.id
INNER JOIN Municipalities AS m ON u.municipalityId = m.id
INNER JOIN Prefectures AS p ON m.prefectureId = p.id
INNER JOIN Regions AS r ON p.regionId = p.regionId
WHERE (
(w.shortName = 'ES' AND m.id IN (:esMunicipalityIdArray))
OR
(w.shortName = 'JHS' AND m.id IN (:jhsMunicipalityIdArray))
OR
(w.shortName = 'ES' AND p.id IN (:esPrefectureIdArray))
OR
(w.shortName = 'JHS' AND p.id IN (:jhsPrefectureIDArray))
OR
(w.shortName = 'ES' AND r.id IN (:esRegionIdArray))
OR
(w.shortName = 'JHS' AND r.id IN (:jhsRegionIdArray))
)
基本上,这里的目标是获取市政府,县(如州或省)或地区的用户的所有姓名(和电子邮件地址)。用户可以选择区域(包含许多县)中的所有用户,或者选择县内的所有用户,或者选择所选城市中的用户。这样,您就可以微调谁收到发送给用户的特定电子邮件。是的,这是相当精细的,是的,它可能有点矫枉过正,但是,嘿,我喜欢挑战。
无论如何,我将如何在Doctrine 1.2中做到这一点?我不确定如何进行这样的嵌套和/或声明。
答案 0 :(得分:1)
使用DQL,以下语句将为:
$query = Doctrine_Query::create()
->distinct(true)
->from("Users u")
->innerJoin("u.Workplaces w on u.workplaceId = w.id)
->innerJoin("u.Municipalities m on u.workplaceId = m.id)
->innerJoin("m.Prefectures p on m.workplaceId = p.id)
->innerJoin("p.Regions r on p.workplaceId = r.id)
->Where("w.shortName = 'ES' and m.id in ($esMunicipalityIdArray))
->orWhere("w.shortName = 'JHS' and m.id in ($jhsMunicipalityIdArray))
->orWhere("w.shortName = 'ES' and p.id in ($esPrefectureIdArray))
->orWhere("w.shortName = 'JHS' and p.id in ($jhsPrefectureIdArray))
->orWhere("w.shortName = 'ES' and r.id in ($esRegionIdArray))
->orWhere("w.shortName = 'JHS' and r.id in ($jhsRegionIdArray));