我正在使用knppaginator对非常大的实体集合进行分页,在对性能不满意之后,我意识到无论传递什么页码,它们都会获取所有实体,我检查了查询分析器,但我从未看到任何偏移量查询。无论如何,这就是我发现的有效方法,但我想将获得计数和实体的部分移回存储库类,我想在其中使用createQueryBuilder。
public function indexAction(Request $request)
{
$page = $request->get('p', 1);
$this->breadcrumbs(
array(
array('title'=>'Admin','path'=>'home'),
array('title'=>'Customers','path'=>'customers'),
array('title'=>'All'),
)
);
$em = $this->getDoctrine()->getManager();
$count = $em
->createQuery('SELECT COUNT(cb) FROM MWCoreBundle:ContactsBasics cb LEFT JOIN cb.login l LEFT JOIN cb.setting s')
->getSingleScalarResult()
;
$entities = $em
->createQuery('SELECT cb,l,s FROM MWCoreBundle:ContactsBasics cb LEFT JOIN cb.login l LEFT JOIN cb.setting s ORDER BY cb.dateAdded DESC')
->setHint('knp_paginator.count', $count);
$pagination = $this->get('knp_paginator')->paginate($entities, $page, 50, array('distinct' => false));
return $this->render('MWCoreBundle:ContactsBasics:index.html.twig', array(
'entities' => $entities,
'pagination' => $pagination,
));
}