Symfony2分页控制器错误

时间:2016-12-10 06:25:48

标签: php symfony pagination

我正在尝试对产品列表进行分页,但我遇到了错误。以下是indexAction文件中的ProductsController.php

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并引用下面的一行:

$total_products = $em->getRepository('IbwTazJazBundle:Products')->countActiveProducts($entities->getId());

我不确定我做错了什么。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

get()返回一个对象集合。这意味着即使您的表中只有一个对象,它也会返回一个1元素的数组,如findAll()

由于您的表名为产品,我怀疑您只有一行。