1

我正在使用一个使用 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 中执行此操作?我不知道如何去做这样的嵌套和/或语句。

4

1 回答 1

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));
于 2013-01-25T19:46:10.547 回答