Search code examples
phpsymfonyormdoctrinefosuserbundle

Symfony FosUserBundle show only user with role == ROLE_USER


I am trying to get only user with ROLE_USER.

I have this in my controller:

$query = $this->getDoctrine()->getEntityManager()
->createQuery(
        'SELECT u FROM StageUserBundle:User u WHERE u.roles LIKE :role'
)->setParameter('role', '%"ROLE_USER"%' 
);
$users = $query->getResult();

It worked with ROLE_ADMIN instead of ROLE_USER but I need to show only User.getRoles()== "ROLE_USER".


Solution

  • I Found the solution to my problem . i changed this in my controller :

    $query = $this->getDoctrine()->getEntityManager()
        ->createQuery('SELECT u FROM StageUserBundle:User u WHERE NOT u.roles LIKE :role'
        )->setParameter('role', '%"ROLE_ADMIN"%' );
    
        $users = $query->getResult();
    

    Hope this will help.