Khalil
Khalil

Reputation: 388

How to use orWhere in Doctrine | Return user who have ROLE_ADMIN and ROLE_USER

I want to return user who have ROLE_ADMIN and ROLE_USER

I have did this in repository :

return $this->createQueryBuilder('u')
        ->where('u.roles IN (:val)')
        ->setParameter('val','["ROLE_ADMIN","ROLE_USER"]')
        ->getQuery()
        ->getResult();

But nothing is returned... How to solve this probleme?

ps: I have a user with ROLES : ROLE_ADMIN and ROLE_USER

Upvotes: 1

Views: 69

Answers (1)

Imanali Mamadiev
Imanali Mamadiev

Reputation: 2654

To use orWhere in Doctrine 2:

return $this->createQueryBuilder('u')
        ->where('u.roles LIKE :val')
        ->setParameter('val','%ROLE_ADMIN%')
        ->orWhere('u.roles LIKE :val2')
        ->setParameter('val2', '%ROLE_USER%')
        ->getQuery()
        ->getResult();

Also You can use like this:

return $this->createQueryBuilder('u')
        ->where('u.roles LIKE :val')
        ->orWhere('u.roles LIKE :val2')
        ->setParameters(array('val2' => '%ROLE_USER%', 'val' => '%ROLE_ADMIN%'))
        ->getQuery()
        ->getResult();

Upvotes: 1

Related Questions