Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8226277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:42:30+00:00 2026-06-07T15:42:30+00:00

I have two entities: Article and Category. That have a many to many table:

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:42:32+00:00Added an answer on June 7, 2026 at 3:42 pm

    I was able to figure it out. I used DQL to create the query:

        $qb = $this->getEntityManager()->createQueryBuilder();
        $qb->select('COUNT(c) cnt, c.id, c.name')
        ->from('NatknowEditorBundle:Category', 'c')
        ->innerJoin('c.articles', 'ac')
        ->groupBy('c.id')
        ->orderBy('cnt', 'DESC');
    
        return $qb->getQuery()->getResult();
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two entities named Category and Article which have a many to many
i have two Entities, Parent and Child, that are linked through a bidirectional one-to-many
I have two entities. Groups. Pools. A Group can create many pools. So I
I have two entities that have a many-to-many relationship; they are mapped with annotations
I have two entities that usually have one-to-many relationship, but in rare cases should
I have two entities with many-to-many relationship defined on them. <set name=TreasuryCodes table=ServiceProviderAccountTreasuryCode lazy=true
I have two entities Group and Contact. A contact can belong to many groups,
I have two entities referenced one to many. When entity framework created the table
Let's say I have two entities in my Symfony2 project : Category and Article
I have two entities: @Entity Article { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.