0

here is my code to retreive users from db :

$qb = $this->createQueryBuilder('c');

    $qb->select('c.username')
        ->where('c.username LIKE :term')
        ->setParameter('term', '%' . $term . '%')
        ->where('c.roles LIKE :role')
        ->setParameter('role', '%"ROLE_COM"%');

    $arrayAss = $qb->getQuery()
        ->getArrayResult();

I want to select only users with the admin role and nothing works.

thaks for helping

ibradev
  • 45
  • 11
  • 2
    Possible duplicate of [FOS bundle - How to select users with a specific role?](https://stackoverflow.com/questions/9016914/fos-bundle-how-to-select-users-with-a-specific-role) – Alan T. Jan 29 '18 at 15:41
  • Thnaks for the url i didnot found it. ive edited the code but it seems that i cant do it with 2 parameters in each line i must combine them into one line. Loot top for code – ibradev Jan 29 '18 at 15:47
  • you need to use `andWhere` and not `where` so that the where clauses can stack – Alan T. Jan 29 '18 at 15:50
  • Thanks it works perfectly :) Add an answer to close this – ibradev Jan 29 '18 at 15:55

3 Answers3

0

Try with isGranted('ROLE_ADMIN') method.

For further information about this method: https://symfony.com/doc/current/security.html

doulouM
  • 41
  • 6
0

Issue solved this way :

        $qb->select('c.username')
        ->where('c.username LIKE :term AND c.roles LIKE :role')
        ->setParameter('term', '%' . $term . '%')
        ->andWhere('c.roles LIKE :role')
        ->setParameter('role', '%"ROLE_COM"%')
    ;
ibradev
  • 45
  • 11
0

The role param should not have quotes and the second "where" should be "andWhere". Try it like this it should work.

$qb->select('c.username')
    ->where('c.username LIKE :term')
    ->setParameter('term', '%' . $term . '%')
    ->andWhere('c.roles LIKE :role') // andWhere for second conditional
    ->setParameter('role', '%ROLE_COM%'); // Lose the quotes here.

$arrayAss = $qb->getQuery()
    ->getArrayResult();
Kolgrim
  • 61
  • 5