简体   繁体   中英

Pagerfanta does not like my doctrine query

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?

为了使其工作,只需删除“-> getQuery()-> getResult();”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM