Query on a many-to-many relationship using Doctrine with Symfony2

后端 未结 1 636
夕颜
夕颜 2020-12-03 14:19

I\'m triyng to understand how the many to many relationship works with Doctrine and Symfony2.

I\'ve recreated the example shown in the official documentation (goo.gl

相关标签:
1条回答
  • 2020-12-03 15:01

    You can write a join DQL query as below

    $em = $this->getContainer()->get('doctrine')->getManager();
    $repository = $em->getRepository('YourNamespaceYourBundle:User');
    $query = $repository->createQueryBuilder('u')
        ->innerJoin('u.groups', 'g')
        ->where('g.id = :group_id')
        ->setParameter('group_id', 5)
        ->getQuery()->getResult();
    

    Your mapping for groups property in User entity will handle join part itself you don't have to mention the junction table in your DQL query

    0 讨论(0)
提交回复
热议问题