Mit
Mit

Reputation: 349

Doctrine querybuilder that uses the object's method

I need to build a query that returns all the users that got 'ROLE_ADMIN' as role (I use FOSUserBundle for that).

Il already tried this but it doesn't work, it throws me "Warning: get_class() expects parameter 1 to be object, boolean given".

public function getAdminQueryBuilder() {

      return $this->createQueryBuilder('u')
              ->where('u.hasRole(:role)', true)
              ->setParameter('role', 'ROLE_ADMIN')
      ;
    }

Note that without the where condition, it works and I got all users in dabatase returned.

Upvotes: 1

Views: 119

Answers (1)

Alvin Bunk
Alvin Bunk

Reputation: 7764

This is how you should call the where:

public function getAdminQueryBuilder() {
    return $this->createQueryBuilder('u')
              ->where('u.hasRole = :role')
              ->setParameter('role', 'ROLE_ADMIN')
    ;
}

Let me know if there are issues.


EDIT # 2

Try this instead:

public function getAdminQueryBuilder() {
    return $this->createQueryBuilder('u')
              ->where('u.roles LIKE :roles')
              ->setParameter('roles', '%ROLE_ADMIN%')
    ;
}

I think it should work, but not certain.

Upvotes: 2

Related Questions