0

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

Community
  • 1
  • 1
Abdelaziz Dabebi
  • 1,624
  • 1
  • 16
  • 21

4 Answers4

2

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.

Franckentien
  • 324
  • 6
  • 21
0

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().

MeuhMeuh
  • 826
  • 6
  • 26
  • changed to $repository2=$em->getRepository('OCUserBundle:User'); $users=$repository2->findByRoles('ROLE_ADMINISTRATEUR'); and in the UserRepository: 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(); } Nothinh happens, the page is still empty. – Abdelaziz Dabebi Jul 30 '15 at 11:45
  • Did you try to debug the $users variable to see if there was anything inside ? Also, check if the SQL generated by the query ($qb->getSql() and $qb->getParameters()) seems fine for you. – MeuhMeuh Jul 30 '15 at 12:13
0

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.

Abdelaziz Dabebi
  • 1,624
  • 1
  • 16
  • 21
0

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.