I’m using Doctrine 2 Paginator and i’m experiencing a bug (maybe) with Twig. Consider a simple Paginator initialization:
$current = 1;
$limit = 5;
$offset = ($current - 1) * $limit;
$qb->setFirstResult($offset)->setMaxResults($this->limit);
// No fetch joins
$items = new \Doctrine\ORM\Tools\Pagination\Paginator($qb->getQuery, false);
// Total count
var_dump($items->count()); // Prints 8
// Number of items displayed
var_dump(count($items)); // Prints 5
// Items
foreach($items as $item) :
var_dump($items->getId()); // Prints 1, 2, 3, 4, 5
endif;
Counts are just fine to me. But after assigning it to Twig with array('items' => $items):
{% for item in items %}
{{ loop.index }}/{{ loop.length }}
{% endfo %}
Output is wrong, in particular loop.length refers to the entire collection (not the current set of items). So, for example, you can’t use loop.last:
1/8
2/8
3/8
4/8
5/8
Self answer. This code from Doctrine documentation has led me astray:
Wrong. You have to assign the iterator to Twig, not the paginator instance itself:
EDIT: sorry i found that
count($paginator) == $paginator->count(), so current items count is$paginator->getIterator()->count().