Q:

QueryBuilder and entity with ManyToMany relation

In the database, I have a table User (idUser, name...) and a table Role(IdRole, description..).
Between the two of them there is a link table HasRole (idUser, idRole).
Generating the entity with the Symfony command line, the HasRole entity is not generated. Instead, in the User Entity I have this on the role property :
* @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="MyBundle\Entity\Role", inversedBy="utilisateur")
 * @ORM\JoinTable(name="hasrole",
 *   joinColumns={
 *     @ORM\JoinColumn(name="UTILISATEUR", referencedColumnName="ID")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="ROLE", referencedColumnName="ID")
 *   }
 * )

I would like to create in the UserRepository a method that returns all of the users for one id Role.
It should starts like this :
$qb = $this->getEntityManager()->createQueryBuilder()
        ->select('utilisateur')
        ->from('ACCUEILBundle:User', 'utilisateur');

can u help ?
Ty

A:

If you use Doctrine to generate your ManyToMany join table, then indeed you will not have an entity for that.
If you need to have the join table as an entity (for example because you store additional data in that table), then you should create relationship in the following way:
entityA <-OneToMany-> joinEntity <-ManyToOne-> entityB

However in your case, it should suffice to just get the Role with the desired ID and then get the users from that:
$role = $entityManager->getRepository('Role')->find($id);
$users = $role->getUsers(); //(or getUtilisatuers(), if I see correctly)

