Question

I'm working with a legacy system that uses Doctrine 1.2. I'm trying to create a query with multiple nested conditions that look something like this:

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))
)

Basically, the goal here is to get all the names (and email addresses) for users in the municipalities, prefectures (like states or provinces), or regions. The user can select all the users in a region (which contains many prefectures), or all the users in a prefecture, or users in selected municipalities. That way, you can fine-tune who gets a specific email sent to users. Yes, this is rather fine-grained, and yes, it may be overkill, but hey, I like the challenge.

Anyway, how would I go about doing this in Doctrine 1.2? I'm not sure how to go about doing such nested and/or statements.

Était-ce utile?

La solution

Using DQL, the below statement will be:

$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));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top