In FosUserbundle, How can I find users by specific role. I've also implemented Group so Roles array come from Group->getRoles too. Is there a way to use QueryBuilder not loop through all users from db?
Asked
Active
Viewed 3,590 times
1
-
I think this might answer your question: http://stackoverflow.com/questions/9016914/symfony-2-fos-bundle-how-to-select-users-with-a-specific-role – Tocacar Jul 23 '13 at 09:27
-
1The problem here is that User has one to many relationship to Group, and in each group has an roles array. I've already saw this answer but it just helps for simple case without Group entity – Tuan nguyen Jul 23 '13 at 10:39
1 Answers
3
So you just need to add a join from User to Group to Roles, where role=the role you're looking for. It's just a straight forward query with a join.
Something like this:
$qb->select("u")
->from("YourUserBundle:User", "u")
->join("u.Group", "g")
->join("g.Role", "r")
->where("r.role = :role");
$query = $qb->getQuery();
$query->setParameter("role", $role);
$results = $query->getResult();

Carrie Kendall
- 11,124
- 5
- 61
- 81

Tocacar
- 475
- 3
- 12