简体   繁体   中英

Pagerfanta symfony Pagination

I have a mysql database blog with 21 posts in it. I set $maxPerPage = '3'; So I get 7 pages, but from page 4 I get the following error:

OutOfRangeCurrentPageException: Page "4" does not exist. The currentPage must be inferior to "3"

Here's my code:

  public function indexAction($page)

{

$maxPerPage = '3';
$currentPage = $page;

$entityManager = $this->getDoctrine()->getManager();
$queryBuilder = $entityManager->createQueryBuilder()
->select(array('u')) // string 'u' is converted to array internally
->from('AppBundle:Blog', 'u');
$adapter = new DoctrineORMAdapter($queryBuilder);
$pagerfanta = new Pagerfanta($adapter);
if (!$page) $currentPage = '1';
try  {
    $pagerfanta->setCurrentPage($currentPage);
}
catch(NotValidCurrentPageException $e) {
  throw new NotFoundHttpException('Illegal page');
}
$pagerfanta->setMaxPerPage($maxPerPage); // 10 by default






//$textblog = $this->getDoctrine()
  //->getRepository('AppBundle:Blog')
  //->find($page);


return $this->render('textblog/blog.html.twig',array('pagerfanta' => $pagerfanta));

}

Can anyone help please?

Stupid me...

$pagerfanta->setMaxPerPage($maxPerPage);

This code should be before the 'if'

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM