Pagerfanta does not like my doctrine query

330 Views Asked by At

In an application built on the symfony 4 framwork, I use a query in my user repository to find users with a specific role, that looks like this:

public function findByRole($role)
{
    $qb = $this->_em->createQueryBuilder();
    $qb->select('u')
        ->from($this->_entityName, 'u')
        ->where('u.roles LIKE :roles')
        ->setParameter('roles', '%"'.$role.'"%');

    return $qb->getQuery()->getResult();
}

Now this query works well in general, but whenever I use it in combination with pagerfanta i encounter a problem. For example whenever I use the following in a controller:

    $em = $this->getDoctrine()->getManager();
    $query = $em->getRepository(User::class)->findByRole('ROLE_ADMIN');

    $pagerfanta = $this->paginate($request, $query);

I get the error: "Call to a member function setFirstResult() on array".

To get around this problem I use:

    $em = $this->getDoctrine()->getManager();
    $query = $em->getRepository(User::class)->createQueryBuilder('u')
                ->andWhere( 'u.roles LIKE :role')
                ->setParameter('role', '%"ROLE_ADMIN"%');

    $pagerfanta = $this->paginate($request, $query);

This works with pagerfanta without any problems. I just do not understand what is wrong with the first query (which does work whenever I do not use pagerfanta). Any ideas?

1

There are 1 best solutions below

0
On

In order to make it work, just remove "->getQuery()->getResult();".