[英]Symfony2 Pagination Controller Error
我正在尝试对产品列表进行分页,但是遇到错误。 这是ProductsController.php
文件中的indexAction
。
public function indexAction($page)
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('IbwTazJazBundle:Products')->findAll();
$total_products = $em->getRepository('IbwTazJazBundle:Products')->countActiveProducts($entities->getId());
$products_per_page = $this->container->getParameter('max_products_on_homepage');
$last_page = ceil($total_products / $products_per_page);
$previous_page = $page > 1 ? $page - 1 : 1;
$next_page = $page < $last_page ? $page + 1 : $last_page;
$entities->setActiveProducts($em->getRepository('IbwTazJazBundle:Products')->getActiveProducts($entities->getId(), $products_per_page, ($page - 1) * $products_per_page));
return $this->render('IbwTazJazBundle:Products:index.html.twig', array(
'entities' => $entities,
'last_page' => $last_page,
'previous_page' => $previous_page,
'current_page' => $page,
'next_page' => $next_page,
'total_products' => $total_products
));
}
刷新页面时,我收到一条Error: Call to a member function getId() on a non-object
消息Error: Call to a member function getId() on a non-object
并引用下面的行:
$total_products = $em->getRepository('IbwTazJazBundle:Products')->countActiveProducts($entities->getId());
我不确定自己在做什么错。 有什么建议么?
findAll()
返回对象的Collection。 这意味着即使表中只有一个对象,它也将返回由1个元素组成的数组,例如[0=> Product]
由于您的桌子被命名为产品,我怀疑您只有一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.