I've got a custom user provider entity which permits me to connect the users. This custom user provider entity implements UserInterface in accordance to this interface I've got a "getRoles()" function which give me the user's roles.
But my roles are recursive.
Example: a user got a role1, the role1 inherited the role2 so the user has gotten the role1 and the role2. To make this recursivity I create a role table, a role_role table (parent/child), a user table and finally a user_role table.
To get ALL the user's roles I have to query my DB with Doctrine so from where can I do that ?
It seems to be forbidden to query from an entity and I can't put the query in entity repository classe because I can't overwrite the entity's "getRoles()" and it seems not to be a good idea to access the repository form entities.
P.-S.: thank you for your indulgence with my grammar, it's my first English message (I'm French).
There is no role hierarchy in DB in Symfony 2.x. It's configure in security.yml check out http://symfony.com/doc/current/book/security.html#hierarchical-roles .
If you don't want to use this feature straight from Symfony 2, you'll have to implement yourself a RoleVoter that get the roles hierarchy direct from the DB.
Another possibility is to use Doctrine Events Listeners ( http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html ) to load the hierarchy from the DB. You'll probably need to listen to the postLoad event.
The solution is to use fully the Doctrine's ORM.
Add a collection variable in the User entity which reference all the UserRole linked to him with an ORM:
@ORM\OneToMany(targetEntity="RoleUser", mappedBy="user")
...
protected $roleUser;
In RoleUser entity add an ORM which permit to bind the User with the right RoleUser:
@ORM\ManyToOne(targetEntity="User", inversedBy="roleUser")
...
protected $user;
Now you can get all the UserRoles linked to the User.
To get the roles write the "sames" ORM between UserRole entity and Role and between RoleRole and Role. Finally you access to your roles from User by $rolesUtilisateur.