I am trying to paginate a list of products but I'm coming across an error. Here is the indexAction
from the ProductsController.php
file.
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
));
}
When I refresh the page, I'm getting a message Error: Call to a member function getId() on a non-object
and it is quoting the line below:
$total_products = $em->getRepository('IbwTazJazBundle:Products')->countActiveProducts($entities->getId());
I'm not sure what I'm doing wrong. Any suggestions?
findAll()
return a Collection of object. It mean even if you have only one object in your table, it will return an array of 1 Element like [0=> Product]
As your table is named products, I doubt you have only one row.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.