I’m having problem with sorting my query results by the count field. My simplified query:
$customers = Doctrine_Core::getTable("Customer")->createQuery("c")
->leftJoin("c.Project p")
->orderBy("COUNT(p.id) ASC");
->execute();
I am pretty sure that the problem isn’t doctrine related. The ORM-generated query retrieved from the application logs selects only one row, and it contains a line:
ORDER BY COUNT( r2.id ) ASC
After removing which all rows are selected properly.
The ORDER BY clause causes the query to select only one row from the customers table. I think it has something to do with the customers not having any projects assigned to them. What am I doing wrong?
If you are trying to group by the number of Projects for a Customer, try using a GROUP BY clause and aggregate on the primary key of Customer: