Doctrine2 Order By before Group By

2019-04-14 01:28发布

问题:

I am having issues implementing a sub-select solution for ORDERING a resulting dataset before the GROUP BY reduces it.

Normally, in SQL you would do a sub-select:

SELECT * FROM (
    SELECT * FROM a_table order by a_table.timestamp desc
) as table_tmp
group by userId

However, I am having difficulty implementing this in DQL. Can anyone point me in the right direction please?

My query is more complex than this and I assume I JOIN other tables through 'table_tmp' and in the outer SELECT.

Thanks.

回答1:

I'm afraid DQL isn't able to handle such a complex query. However Doctrine allows you to write a custom SQL query and map its results into objects.

Read more about native queries on Doctrine's documentation pages.