简体   繁体   English

Pagerfanta不喜欢我的学说查询

[英]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: 在基于symfony 4框架构建的应用程序中,我在用户存储库中使用查询来查找具有特定角色的用户,如下所示:

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. 现在,此查询通常可以正常运行,但是每当我将其与pagerfanta结合使用时,都会遇到问题。 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". 我收到错误:“在数组上调用成员函数setFirstResult()”。

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. 这与pagerfanta一起使用不会有任何问题。 I just do not understand what is wrong with the first query (which does work whenever I do not use pagerfanta). 我只是不明白第一个查询出了什么问题(每当我不使用pagerfanta时它就会起作用)。 Any ideas? 有任何想法吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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