I have a user entity. It implements the basic user-interface.
I want to sort my users by roles and I don't know how to do it.
(it is for send a message for my admins only with a message entity who is targeting the user entity on a ManyToOne relation)
OK, maybe that:
// $users - users from db, collection of user entity class
$myAdmins = array();
foreach($users as $user){
if (in_array('ROLE_ADMIN', $user->getRoles())) {
$myAdmins[] = $user;
}
}
in $myAdmins
has users with ROLE_ADMIN
role.
$query = $this->getDoctrine()->getEntityManager()
->createQuery(
'SELECT u FROM MyBundle:User u WHERE u.roles LIKE :role'
)->setParameter('role', '%"ROLE_MY_ADMIN"%');
$users = $query->getResult();
Offload the work to your DB Server. This query shouldn't take long to return, even with 100,000+ records.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.