我尝试使用捆绑包 KnpPaginator 在我的网站上创建分页。在我的存储库中,我创建了一个查询:
public function getProductsOrderByDateDesc($id_category = null, $max = null){
$qb = $this->createQueryBuilder('p')
->orderBy('p.created_at', 'DESC');
if($max) {
$qb->setMaxResults($max);
}
if($id_category) {
if(is_array($id_category)){
$aIdCategory = implode("','",$id_category);
$qb->andWhere('p.category IN (:ids)')
->setParameter('ids', $aIdCategory);
}else{
$qb->andWhere('p.category = :category_id')
->setParameter('category_id', $id_category);
}
}
$query = $qb->getQuery();
return $query->getArrayResult();
}
在我的控制器中,我这样做:
$repositoryProduct = $em->getRepository('ShopDesktopBundle:Product');
$aProducts = array();
$aProducts = $repositoryProduct->getProductsOrderByDateDesc($id);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$aProducts,
$this->get('request')->query->get('page', 1),
3
);
return $this->render('ShopDesktopBundle:Category:category.html.twig',array(
'aProducts' => $aProducts,
'pagination' => $pagination
));
在视图中我只显示这个分页:
<div class="navigation">
{{ knp_pagination_render(pagination) }}
</div>
问题是始终显示所有产品,不仅限于我的示例为 3。例如:我有 9 个产品,限制 = 3,分页是正确的“1 2 3”,但对于每个页面,我都看到所有 9 个产品帮助请给我!提前谢谢