Symfony2 - paginator bundle 复杂查询时候报错解决

使用paginator bundle组件创建分页时候 在queryBuilder加入了join group等等查询 会出现以下报错:

Cannot count query which selects two FROM components, cannot make distinction


解决办法:

<?php
$paginator = new Paginator;$count = $entityManager
    ->createQuery('SELECT COUNT(c) FROM Entity\CompositeKey c')
    ->getSingleScalarResult();

$query = $entityManager
    ->createQuery('SELECT c FROM Entity\CompositeKey c')
    ->setHint('knp_paginator.count', $count);

$pagination = $paginator->paginate($query, 1, 10, array('distinct' => false));

Distinction in this case also cannot be determined by paginator. It will take direct result of the query and limit with offset. In some cases you may need to use GROUP BY

你可能感兴趣的:(Bundle,paginator)