This is a new version of Paginator Bundle which has been made reusable, extensible, highly customizable and simple to use Symfony2 paginating tool based on Zend Paginator.
- Doctrine ORM active bundle.
- View helper for simplified pagination templates.
- Supports multiple paginators during one request
Notice: using multiple paginators requires setting the alias for adapter in order to keep non conflicting parameters. Also it gets quite complicated with a twig template, since hash arrays cannot use variables as keys.
Add to /deps file:
[ZybreakPaginatorBundle]
git=https://github.com/zybreak/PaginatorBundle.git
target=bundles/Zybreak/PaginatorBundle
Run in console
php bin/vendors install
// app/autoload.php
$loader->registerNamespaces(array(
'Zybreak' => __DIR__.'/../vendor/bundles',
// ...
));
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new Zybreak\PaginatorBundle\ZybreakPaginatorBundle(),
// ...
);
}
$em = $this->get('doctrine.orm.entity_manager');
$dql = "SELECT a FROM VendorBlogBundle:Article a";
$query = $em->createQuery($dql);
$adapter = $this->get('zybreak_paginator.adapter');
$adapter->setQuery($query);
$adapter->setDistinct(true);
$paginator = new \Zybreak\PaginatorBundle\Paginator\Paginator($adapter);
$paginator->setCurrentPageNumber($this->get('request')->query->get('page', 1));
$paginator->setItemCountPerPage(10);
$paginator->setPageRange(5);
<table>
{% for article in paginator %}
<tr {% if loop.index is odd %}class="color"{% endif %}>
<td>{{ article.id }}</td>
<td>{{ article.title }}</td>
</tr>
{% endfor %}
</table>
{# display navigation #}
<div id="navigation">
{{ paginator|paginate }}
</div>