findBy with JOIN criteria in Symfony2

前端 未结 1 1622
悲&欢浪女
悲&欢浪女 2021-01-05 05:37

I have 3 simple tables: user, role, user_x_role with Many-To-Many relation. I have 2 entities: User and Role. User entity has $userRoles property with relation annotation. I

相关标签:
1条回答
  • 2021-01-05 06:20

    IMO the best way for it is create your own repository for User entity. Then in that repository create method like "getUsersByRole" where you make the query you want with query builder.

     $qb = $this->getEntityManager()->createQueryBuilder();
     $qb->select('u')
         ->from('\namespace\for\User', 'u')
         ->join('u.roles', 'r')
         ->where(...)
    
     return $qb->getQuery()->getResult();
    
    0 讨论(0)
提交回复
热议问题