I’m currently using Symfony2 framework. I don’t know how to join 2 of my query by Query Builder in Symfony2, I just can join them using SQL UNION query. Below is the query that returns correct results.
SELECT * FROM (SELECT m.id, m.subject, m.date
FROM message m JOIN message_incoming mi ON m.id = mi.id
WHERE m.recipient_id = 1
AND mi.trash = 1
AND mi.deleted = 0) AS y
UNION
SELECT * FROM (SELECT m.id, m.subject, m.date
FROM message m JOIN message_outgoing mo ON m.id = mo.id
WHERE m.originator_id = 1
AND mo.trash = 1
AND mo.sent = 1
AND mo.deleted = 0) AS z
ORDER BY date DESC
I was trying to join this code in just 1 query (without UNION) to get the correct result but I failed.
So how can I implement this query using query builder?
Please advise, thanks.
I don’t know how stupid I am when joining 2 queries without checking the conditions.
This is the correct query :
I tried implement it through query builder :
How surprisingly it returns incorrect result ! So i tried to see what query was generated using :
And I really don’t know why doctrine generates extra parenthesis as I get this result (carefully have a look at WHERE clause) :
So this must be the correct one if I add my own parenthsis :
Feel a little stupid.