Search code examples
phpsymfonydoctrinedql

Check if id exists in foreign table twice


After trying for a while I thought I'd try to ask here for a change.
I am trying to check if a person id exists in two tables, for one table it works like a charm, but if I try to check another table I get the following error:

[Semantical Error] line 0, col 268 near 'owner FROM \...\Entity\Resource':
Error: Invalid PathExpression. Must be a StateFieldPathExpression.

The trick is that I can only use one DQL query, and the following is what I came up with (... not in actual query):

SELECT contact_person
FROM \\...\Entity\Person contact_person
WHERE NOT EXISTS (SELECT b.personId FROM \\...\Entity\Booking b WHERE b.personId = contact_person.id)
AND NOT EXISTS (SELECT r.owner FROM \\...\Entity\Resource r WHERE r.owner = contact_person.id)

Solution

  • I eventually solved it using subqueries. Joins might be much faster but I lose track of them. (And they gave me a huge headache in DQL)

    Anyway the way I solved it is like this:

    SELECT contact_person
    FROM ...
    WHERE (SELECT COUNT(b.personId) FROM \\...\Entity\Booking b WHERE b.personId=contact_person.id)=0
    AND (SELECT COUNT...)=0
    

    Do note that my code does not need to run real time, so I care for easy of use more than performance. (I like subqueries because they clearly seperate the different rules, I guess other people might like joins better for a good reason aswell, next to performance that is).