1

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)

4

2 回答 2

0
$query = $this->getDoctrine()->getEntityManager()
            ->createQuery(
                'SELECT u FROM MyBundle:User u WHERE u.roles LIKE :role'
            )->setParameter('role', '%"ROLE_MY_ADMIN"%');

$users = $query->getResult();

将工作卸载到您的数据库服务器。即使有 100,000 多条记录,此查询也不应该花很长时间才能返回。

于 2013-03-18T15:30:16.807 回答
0

好吧,也许是这样:

// $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拥有具有ROLE_ADMIN角色的用户。

于 2013-03-18T11:26:47.117 回答