I have two entities: Article and Category. That have a many to many table: article_category.
I can perform this query with no problems in MySQL:
SELECT COUNT(*) cnt, c.id, c.name FROM article_category ac, category c WHERE ac.category_id = c.id GROUP BY c.id ORDER BY cnt DESC
But I am having trouble translating that into a Doctrine2 query. The following returns a class doesn’t exist error, in reference to the article_category table.
[...]
createQuery("SELECT COUNT(*) cnt, c.id, c.name FROM article_category ac, category c WHERE ac.category_id = c.id GROUP BY c.id ORDER BY cnt DESC")
[...]
To specify, my issue is that I don’t know how to access the “category_id” in the many-to-many table.
EDIT
Here is the same query using an inner join (if it makes it easier).
SELECT COUNT(*) cnt, c.id, c.name FROM category c INNER JOIN article_category ac ON c.id = ac.category_id GROUP BY c.id ORDER BY cnt DESC
I was able to figure it out. I used DQL to create the query:
I thought I had to specify where the JOIN needed to occur (“… ON c.id = ac.category_id …”). However, since the many-to-many is already mapped by the array collection (in my case “articles”) I have in my “Category” entity, I guess the “ON” is implicit.