8

I have read that Pagerfanta is the pagination plugin of choice for Symfony2, but I am having some trouble getting it to work correctly. I have downloaded the code for pagerfanta and PagerfantaBundle and installed them as described in the WhiteOctober Github readme file and in this tutorial. I'm pretty sure the code in my controller is okay, but there is something wrong with my template file.

My controller code:

public function indexAction($page =  null)
{
    ...
    $query = $em->createQuery('SELECT something FROM some_table');
    $adapter = new DoctrineORMAdapter($query);
    $pager =  new Pagerfanta($adapter);
    $pager->setMaxPerPage(10);
    if (!$page)    $page = 1;
    try  {
        $pager->setCurrentPage($page);
    }
    catch(NotValidCurrentPageException $e) {
      throw new NotFoundHttpException('Illegal page');
    }
    return $this->render('MyBundle:MyEntity:index.html.twig', array(
        'pager' => $pager,
    ));
}

In my template:

...
<table>
  {% for object in pager.currentPageResults %}
    <tr>
      <td>{{ object.attribute1 }}</td>
      <td>{{ object.attribute2 }}</td>
      <td>{{ object.attribute3 }}</td>
    </tr>
  {% endfor %}
</table>

{% if pager.haveToPaginate %}
  {{ pagerfanta(pager) }}
{% endif %}

When I go to the page, I get a list of the first 10 objects. However,the rendering of the pager is just PreviousNext as one word (not links and no pages 1, 2, 3, etc).

If I add ?page=2 to the URL, it doesn't display the next 10 items in the list.

I didn't change the route from what is was before I added the pagination code because the documentation says:

The routes are generated automatically for the current route using the variable "page" to propagate the page number.

... but I wonder if incorrect routing is part of the problem. Assuming this is the case, I'm not sure how to proceed with this.

I have been unable to find any instructions other the sources I have linked to above, so it would be great if someone who has implemented this successfully could share the details of how they did it.

Florent
  • 12,310
  • 10
  • 49
  • 58
j_goldman
  • 231
  • 1
  • 5
  • 16
  • 1
    Does you route pattern include a page attribute ? ie `/articles/{page}` – AdrienBrault Sep 10 '12 at 14:36
  • Update: There does seem to be a problem with the controller. Specifically, the DocrtineORMAdapter doesn't work properly (i.e `$pagerfanta->getNbResults()` returns 0 - hence no pages between Prevous and Next) . The documentation states that the parameter passed to the constructor can be a Query or a QueryBuilder, but I suspect only the latter is correct. When I change to `$pagerfanta = new Pagerfanta(new ArrayAdapter($query->getResult())` it returns the correct number of results. @AdrienBrault - I made the change you suggested to the route pattern and now it works, but shouldn't be needed. – j_goldman Sep 11 '12 at 09:21
  • So I am still left with the questions of why the ?page= is not automatically added to the route as per the documentation and why the css isn't applied (now the paginator looks like Previous123Next, with no spaces or formatting) – j_goldman Sep 11 '12 at 09:27

2 Answers2

6

I think your application doesn't know how to parse ?page=2 to the controller action.

Include a {page} in your route (as @AdrienBrault replied), which you wish to have pagination.

like this:

my_index:
  pattern:  /{page}
  defaults: { _controller: MyTestBundle:Object:index, page: 1 }
  requirements:
    _method:  GET
    page: \d+

PagerFantaBundle will (I guess) try to guess the correct route.

Worked for me.

  • This works, but in fact this can be done with query parameters as the OP asked. Just add `Request $request` to the action and get `$page` from `$request->get('page', 1)` – xPheRe Aug 16 '13 at 10:10
  • Request's `get` method goes through all (query, attributes and request) parameters. It's officially advised to avoid it when you know where the parameter comes from: in this case you would need to rather use `$request->query->get('page', 1)`. – Loïc Faugeron Oct 09 '15 at 10:49
1

I had to use 'currenPage' instead fo 'page' in my routing and I found a way to do this :

{{ pagerfanta(pager, 'my_pager_view', {'pageParameter':'[currentPage]'}) }}

Charly
  • 436
  • 4
  • 12