PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 11, 2022

[FIXED] how i select some value equal array in Doctrine "createQueryBuilder"

 May 11, 2022     doctrine, orm, symfony     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing