Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Wrong count in twig loop when using Pagerfanta with DoctrineORMAdapter #190

Open
mahdirabie opened this issue Oct 14, 2015 · 3 comments
Open

Comments

@mahdirabie
Copy link

I'm using

$products = new Pagerfanta(new DoctrineORMAdapter($queryBuilder, true, null));
$products->setMaxPerPage(10);

And my twig looks like,

{% for product in products %}
     LENGTH = {{ loop.length }}
{% endfor %}

I have 200 products but on any page loop.length returns 200 instead of 10, that causes twig loop utils to break.

It seems the problem is from DoctrineORMAdapter or Pagerfanta class which does not return number of current page results as twig loop iterator expects.

What is the solution here? I can happily create a PR if you'd guide me.

\cc @aramalipoor

@sstok
Copy link

sstok commented Oct 14, 2015

{{ product.getNbResults() }} will give the amount of items on the current page.

Changing the functionality of the count() method to not return the total would be a BC break.

@aramalipoor
Copy link

@sstok Another problem is that loop.last doesn't work as expected too. It is comparing current index (0,1,2,etc) with length (always 200).

@ghost
Copy link

ghost commented May 18, 2016

A little (inelegant) hack:
{% set remainingItems = (items.getNbResults() - ((items.currentPage - 1) * items.maxPerPage)) %}

and inside loop:
{% if loop.index == remainingItems %}

Can't find another way to know when I'm in the last iteration.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants