Home > Blockchain >  how i select some value equal array in Doctrine "createQueryBuilder"
how i select some value equal array in Doctrine "createQueryBuilder"

Time:02-24

can you help me with this code There are several columns in the database, one of which is a role, user role value looks like this: ["ROLE_ADMIN"] how can I get all users who have this role?

public function findUserWithRolle(){
    $qb=$this->createQueryBuilder('R');
    $qb->select('R.username')
    ->where('R.roles=["ROLE_ADMIN"]');
    return $qb->getQuery()->getResult();
}

CodePudding user response:

If use "direct LIKE-approach" you could get them like:

 public function findUserWithRolle(string $role = 'ROLE_ADMIN'){
   $qb=$this->createQueryBuilder('R');
   $qb->select('R.username')
      ->andWhere('R.roles LIKE :role')
      ->setParameter('role', '%'.$role.'%');
   return $qb->getQuery()->getResult();
 }

Note: There are possible some pitfalls. With "LIKE-approach". But for your case enough.

  • Related