I have a MySQL command and I cannot find the equivalent in DQL. I am trying to fetch the list most commented posts. Here is the MySQL command :
SELECT posts.id, COUNT(comments.id) AS num
FROM posts
LEFT JOIN comments ON ( posts.id = comments.post_id )
GROUP BY posts.id
Here is the result :
id num
1 8
2 9
3 17
4 7
5 6
6 20
7 7
8 10
9 14
10 7
In DQL, it should be :
SELECT post, COUNT(comment.id) AS num
FROM Entity\Post post
LEFT JOIN post.comments comment
GROUP BY post.id
But this gives :
id num
1 50
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
I don't understand where the 50 comes from and why there is a difference between the 2 results. Could you tell me how to make this join work in Doctrine ?
Post
table and in/decrement it whenever you add/remove a comment. – AirlieWHERE post.date > ?
to get the "hotest" posts for last 24h, last day, last month... I am running these commands withapp/console doctrine:query:dql
andapp/console doctrine:query:sql
. – Mentality