我想用至少 2 个左连接对复杂请求进行分页,但是我正在使用的分页包 (KnpPaginationBundle) 无法告诉 Doctrine 如何计算结果(分页过程需要),并继续拥有这个例外。
Cannot count query which selects two FROM components, cannot make distinction
这是使用 Doctrine QueryBuilder 构建的示例请求。
public function findGroupsByUser(User $user, $listFilter, $getQuery = false, $order = 'ASC')
{
$query = $this->createQueryBuilder('r')
->select('r as main,g')
->select('r as main,g, count(gg) as members')
->leftjoin('r.group', 'g')
->innerjoin('MyBundle:GroupMemberRel', 'gg', 'WITH', 'r.group = gg.group')
->addGroupBy('g.groupId')
->add('orderBy', 'g.name ' . $order);
if ($getQuery == true) {
return $query;
}
return $query->getQuery()->getResult();
}
然后我把这个请求给 knp_paginator 服务,然后我得到了异常
$groupQuery = $this->em->getRepository('MyBundle:GroupMemberRel')->findGroupsByUser($user, $listFilter, true);
$paginator = $this->container->get('knp_paginator');
/* @var $groups Knp\Component\Pager\Pagination\PaginationInterface */
$groups = $paginator->paginate(
$groupQuery, $this->container->get('request')->query->get('page', 1), 10 /* limit per page */
);
关于如何对复杂请求进行分页的任何想法,我很确定这个用例很常见,不想在分页后补充我的结果。