As described in a related question, I have a ViewVersion
entity that contains multiple versions for a given View
. If I want to fetch a list of the "best" viewVersions based on which is the last modified, you can not do a GROUP BY viewId
because you can't control the order these return (e.g. timeMod DESC). So you after days of searching I believe the best way to do this is through a simple INNER JOIN on itself that matches the highest timeMod.
This works really well in MySQL, but how in the world can you do this in Doctrine?
Native MySQL Query
SELECT view_version.*
FROM view_version
#inner join only returns where the result sets overlap, i.e. the one record with highest timeMod
INNER JOIN (
SELECT MAX(timeMod) maxTimeMod, viewId
FROM view_version
GROUP BY viewId
) version ON version.viewId = view_version.viewId AND view_version.timeMod = version.maxTimeMod
#join other tables for filter, etc
INNER JOIN view ON view.id = view_version.viewId
INNER JOIN content_type ON content_type.id = view.contentTypeId
WHERE view_version.siteId=1
AND view.contentTypeId IN (2)
ORDER BY view_version.title ASC
Question
How can you form a Doctrine query to join an entity TO ITSELF, using QueryBuilder or DQL (or if absolutely necessary Native).
Everytime I try, the INNER JOIN
complains about the SELECT statement like so:
QueryException: [Semantical Error] line 0, col 72 near '(SELECT': Error: Class '(' is not defined
NOTE: this is different than my original question, in that this is focused specifically on joining an entity to itself.