Issue
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();
}
Solution
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.
Answered By - voodoo417 Answer Checked By - Cary Denson (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.