symfony 2,分页

时间:2013-04-02 08:25:51

标签: symfony

我刚刚开始使用Symfony2,我在分页方面遇到了一些问题。

我在Account课程中有这段代码:

public function indexAction($page)
{
    $session = $this->get('session');

    if ($session->get('valid')=='true') {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('ProjectCRMBundle:Account')->findAll();


        $user = new Account();
        $form = $this->container->get('form.factory')->create(new AccountType());
        $total = $this->getDoctrine()->getRepository('ProjectCRMBundle:Account')->createQueryBuilder('p')->getQuery()->getResult();

        /* total of résultat */
        $total_users    = count($total);
        $users_per_page = 1;
        $last_page      = ceil($total_users / $users_per_page);
        $previous_page  = $page > 1 ? $page - 1 : 1; 
        $next_page      = $page < $last_page ? $page + 1 : $last_page;

        /* résultat  à afficher*/
        $entities          = $this->getDoctrine()->getRepository('ProjectCRMBundle:Account')->createQueryBuilder('p')->setFirstResult(($page * $users_per_page) - $users_per_page)->setMaxResults(1)->getQuery()->getResult();

        return $this->render('ProjectCRMBundle:Account:index.html.twig', array(
            'entities'      => $entities,
            'last_page'     => $last_page,
            'previous_page' => $previous_page,
            'current_page'  => $page,
            'next_page'     => $next_page,
            'total_users'   => $total_users,
            'form'          => $form->createView(),
            'user'          => $user,
        ));

        return $this->render('ProjectCRMBundle:Account:index.html.twig');
    } else {
        return $this->redirect($this->generateUrl('user_login'));
    }
}

/**
 * Creates a new Account entity.
 *
 */
public function createAction(Request $request)
{
    $entity = new Account();
    $form = $this->createForm(new AccountType(), $entity);
    $form->bind($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('account_show', array('id' => $entity->getId())));
    }

    return $this->render('ProjectCRMBundle:Account:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    )); 
}

不要忘记account.yml

上的路径
account:
    pattern:  /{page}
    defaults: { _controller: "ProjectCRMBundle:Account:index" , page: 1 }

乍一看,它似乎有效,但在尝试添加新帐户后,我收到了此消息:

  

LIMIT参数offset = -1无效

任何人都可以解决我的问题吗?

0 个答案:

没有答案