DDC-3207: Paginator does not count according to query max results #3973

Closed
opened 2026-01-22 14:32:37 +01:00 by admin · 10 comments
Owner

Originally created by @doctrinebot on GitHub (Jul 5, 2014).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user draeli:

I use Doctrine\ORM\Tools\Pagination\Paginator to have an paginate object and use like this :

$paginator = new Paginator($query);
$paginator->getQuery()->setFirstResult(0)->setMaxResults(1);

In this case when I do a count on object result and have "int 2" in spite I expected "int 1".

I look the class and find may be the problem, why there is this ?

$countQuery->setFirstResult(null)->setMaxResults(null);

in "count" method.

If I remove this line, expected result appear.

Originally created by @doctrinebot on GitHub (Jul 5, 2014). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user draeli: I use `Doctrine\ORM\Tools\Pagination\Paginator` to have an paginate object and use like this : ``` $paginator = new Paginator($query); $paginator->getQuery()->setFirstResult(0)->setMaxResults(1); ``` In this case when I do a count on object result and have "int 2" in spite I expected "int 1". I look the class and find may be the problem, why there is this ? ``` $countQuery->setFirstResult(null)->setMaxResults(null); ``` in "count" method. If I remove this line, expected result appear.
admin added the Bug label 2026-01-22 14:32:37 +01:00
admin closed this issue 2026-01-22 14:32:37 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jul 6, 2014):

Comment created by @ocramius:

The paginator will always count over the entire resultset of your query, since that is its primary purpose together with providing iteration over slices of the resultset.

@doctrinebot commented on GitHub (Jul 6, 2014): Comment created by @ocramius: The paginator will always count over the entire resultset of your query, since that is its primary purpose together with providing iteration over slices of the resultset.
Author
Owner

@doctrinebot commented on GitHub (Jul 6, 2014):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Jul 6, 2014): Issue was closed with resolution "Invalid"
Author
Owner

@doctrinebot commented on GitHub (Jul 6, 2014):

Comment created by draeli:

I can understand the global count but for example (and now I saw why the line I descripbe above is) in case I use with Twig for iterate (with {% for %} syntax), the length return by object|length not appear to be what expected because if the goal of Pagination is to expose method to work more easier with object, I supose you must only work with current page object result and the total count must be expose in method but not directly as iterator count result.

@doctrinebot commented on GitHub (Jul 6, 2014): Comment created by draeli: I can understand the global count but for example (and now I saw why the line I descripbe above is) in case I use with Twig for iterate (with {% for %} syntax), the length return by object|length not appear to be what expected because if the goal of Pagination is to expose method to work more easier with object, I supose you must only work with current page object result and the total count must be expose in method but not directly as iterator count result.
Author
Owner

@doctrinebot commented on GitHub (Jul 6, 2014):

Comment created by @ocramius:

[~draeli] the count($paginator) will give you the total items. The current offset and limit can be trusted to get the items in the current page.

@doctrinebot commented on GitHub (Jul 6, 2014): Comment created by @ocramius: [~draeli] the `count($paginator)` will give you the total items. The current offset and limit can be trusted to get the items in the current page.
Author
Owner

@doctrinebot commented on GitHub (Jul 6, 2014):

Comment created by draeli:

I understood about the count($paginator) but that's not the only point.
Why when I do loop.last (inside for loop) this give me "false" in spite it is the last one for the current page ?
Sorry to insist but as simple user of Sf2/Twig/Doctrine I don't understand this logic :/
(or maybe I'm not enough clear)

@doctrinebot commented on GitHub (Jul 6, 2014): Comment created by draeli: I understood about the count($paginator) but that's not the only point. Why when I do `loop.last` (inside for loop) this give me "false" in spite it is the last one for the current page ? Sorry to insist but as simple user of Sf2/Twig/Doctrine I don't understand this logic :/ (or maybe I'm not enough clear)
Author
Owner

@doctrinebot commented on GitHub (Jul 6, 2014):

Comment created by draeli:

To explain more, actually I do a request and give me object :

$paginator = new Paginator($query);
$paginator->getQuery()->setFirstResult(0)->setMaxResults(5);
$Articles = $paginator;

and in the view

` dump(Articles|length) ` {# here "int 23" for example in this case because result without limit is 23, ok I understand #}
{% for anArticle in Articles %}
` dump(loop.first) ` {# as expected here "true" for the first and false for other #}
` dump(loop.last) ` {# always "false" even for the fifth result who is suppose to bu "true" #}
` dump(loop.length) ` {# here return always "int 23" in spite expected 5 #}
{% endfor %}
{# for display my 5 object in spite loop.last and loot.length not expected result #}

As you see something is not expected.

@doctrinebot commented on GitHub (Jul 6, 2014): Comment created by draeli: To explain more, actually I do a request and give me object : ``` $paginator = new Paginator($query); $paginator->getQuery()->setFirstResult(0)->setMaxResults(5); $Articles = $paginator; ``` and in the view ``` ` dump(Articles|length) ` {# here "int 23" for example in this case because result without limit is 23, ok I understand #} {% for anArticle in Articles %} ` dump(loop.first) ` {# as expected here "true" for the first and false for other #} ` dump(loop.last) ` {# always "false" even for the fifth result who is suppose to bu "true" #} ` dump(loop.length) ` {# here return always "int 23" in spite expected 5 #} {% endfor %} {# for display my 5 object in spite loop.last and loot.length not expected result #} ``` As you see something is not expected.
Author
Owner

@doctrinebot commented on GitHub (Jul 6, 2014):

Comment created by @ocramius:

We can't provide support for Twig integration. That seems to be a Twig specific issue with how twig deals with loops, nothing related with the ORM. Instead, ask on http://stackoverflow.com/

@doctrinebot commented on GitHub (Jul 6, 2014): Comment created by @ocramius: We can't provide support for Twig integration. That seems to be a Twig specific issue with how twig deals with loops, nothing related with the ORM. Instead, ask on http://stackoverflow.com/
Author
Owner

@doctrinebot commented on GitHub (Jul 6, 2014):

Comment created by draeli:

Ok thank you for your answer, I'm going to ask, just I signal I put question relative to this for Twig here :
https://github.com/symfony/symfony/issues/11316
Have a good day :)

@doctrinebot commented on GitHub (Jul 6, 2014): Comment created by draeli: Ok thank you for your answer, I'm going to ask, just I signal I put question relative to this for Twig here : https://github.com/symfony/symfony/issues/11316 Have a good day :)
Author
Owner

@doctrinebot commented on GitHub (Jul 7, 2014):

Comment created by stof:

Iterate over paginator.iterator in Twig, and loop.last will give you wht you want

@doctrinebot commented on GitHub (Jul 7, 2014): Comment created by stof: Iterate over `paginator.iterator` in Twig, and `loop.last` will give you wht you want
Author
Owner

@doctrinebot commented on GitHub (Jul 8, 2014):

Comment created by draeli:

A really big thank you Christophe :) It's work :D

@doctrinebot commented on GitHub (Jul 8, 2014): Comment created by draeli: A really big thank you Christophe :) It's work :D
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3973