Abdelaziz Dabebi
Abdelaziz Dabebi

Reputation: 1638

How to get a list of users with specific role symfony controller

I'm new to symfony 2, working with FOSUserBundle along with PUGXMultiUserBundle and I have troubles retrieving the list of users with a specific role, for example : ROLE_ADMINISTRATEUR to notifiy them about something. Anyway, inspired by this this is my UserRepository class:

<?php
namespace OC\UserBundle\Entity;

use Doctrine\ORM\EntityRepository;


class UserRepository extends EntityRepository
{
 public function findByRoles($role)
{
 $qb = $this->_em->createQueryBuilder();
 $qb->select('u')
    ->from('OCUserBundle:User', 'u')
    ->where('u.roles LIKE :roles')
    ->setParameter('roles', '%"'.$role.'"%');

 return $qb->getQuery()->getResult();
 }
}

and this is the code inside in the controller's action:

$em=$this->getDoctrine()->getManager();
$repository2=$em->getRepository('OCUserBundle:User');
$roles='ROLE_ADMINISTRATEUR';
$users=$repository2->findByRoles(array('roles'=>$roles));

Return $this->render('OCUserBundle:Default:test.html.twig',array(
        'users'=>$users));

and my test.html.twig page:

 {% for a in users %}
 {{a.username}}
 {% endfor %}

All what I get is an empty page. Any help would be appreciated

Upvotes: 0

Views: 5225

Answers (4)

Franckentien
Franckentien

Reputation: 324

I try to do this also without PUGXMultiUserBundle with the function:

$users = $repositoryUser->findByRoles("ROLE_SUPER_ADMIN");

But it's doesn't work i get an empty array, so i do it manually:

$qb = $em->createQueryBuilder();
$qb->select('u') ->from('AppBundle:User', 'u') ->where('u.roles LIKE :roles') ->setParameter('roles', '%"'."ROLE_SUPER_ADMIN".'"%');
$users = $qb->getQuery()->getResult();

Maybe this can help people who are in the same case of me.

Upvotes: 2

Jerzy Z Gierzy
Jerzy Z Gierzy

Reputation: 21

I just had the same problem as the OP. Actually the thing with the above piece of code from the OP, are the double quotes: ->setParameter('roles', '%"'.$role.'"%'); These didn't seemed right from the first look. Changed to: ->setParameter('roles', '%'.$role.'%'); and all works fine.

Upvotes: 0

Abdelaziz Dabebi
Abdelaziz Dabebi

Reputation: 1638

Solved. Actually, using PUGXMultiUserBundle, you can select from a particular table, (you can associate a type of user with a role) so I changed the action inside the controller to this:

$em=$this->getDoctrine()->getManager();
$repository2=$em->getRepository('OCUserBundle:UserAdministrateur');
$admins=$repository2->findAll();

Return $this->render('OCUserBundle:Default:test.html.twig',array(
    'admins'=>$admins));

Works like a charm. HOpe this helps someone.

Upvotes: 0

MeuhMeuh
MeuhMeuh

Reputation: 885

Did you try by correcting this line :

$users=$repository2->findByRoles(array('roles'=>$roles));

By :

$users=$repository2->findByRoles('ROLE_ADMINISTRATEUR');

?

Your method seems to search for users for ONE role. The LIKE condition expects a string and not an array.

The method would be then more like findByRole() and not findByRoles().

Upvotes: 0

Related Questions