mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
DDC-3861: Incorrect count in Paginator for queries using GROUP BY #4730
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @doctrinebot on GitHub (Aug 3, 2015).
Jira issue originally created by user sparrowek:
When using group by clause Paginator::count() method returns incorrect result. For example, the count metod produces the following query:
SELECT count(DISTINCT d0_.id) AS sclr_0 FROM diet_templates d0_ WHERE d0_.database_id = 3 AND d0_.type_id = 1
GROUP BY d0_.diet_id, d0_.day, d0_.meal_id
This query returns multiple records because it counts the number of records in groups and not the number of groups, eg. 1,1,1,2.
Since the count() method currently does array_sum like this:
$this->count = array_sum(array_map('current', $this->getCountQuery()->getScalarResult()));
The overall result is 5 (111+2) instead of 4.
When using group by clause the correct result whould be achived with a simple count on the result like this:
$this->count = count($this->getCountQuery()->getScalarResult());
@arnotae commented on GitHub (Dec 31, 2018):
I have exactly the same problem, is there a way to have the right count now ? Thanks ;)
@Ocramius commented on GitHub (Dec 31, 2018):
@arnotae please reproduce this with a test case first.
@karser commented on GitHub (Jan 1, 2019):
I'm stuck with this issue too. I'll see if I can isolate it
@karser commented on GitHub (Jan 1, 2019):
I researched it, here is the explanation.
I want to see only unique types within DietTemplate:
This query I expect Doctrine to perform:
The actual query:
That's because

Doctrine\ORM\Tools\Pagination\CountWalkertakes $rootClass->getSingleIdentifierFieldName() for the COUNT clause:@Ocramius commented on GitHub (Jan 2, 2019):
This seems like expected behaviour, since the paginator iterates over a set of identifiers, and then retrieves matching query results for those identifiers.
The paginator examples in this issue seem to operate over arbitrary scalar values bound to those identifiers (most likely unsupported).
The correct course of action here (since selecting a subset of identifiers is the intended design) is to prevent users from having scalar-only
FROMclauses, or enforcing that such scalars do not come from further aggregation operations (DISTINCT,MAX,COUNT, etc.).@karser commented on GitHub (Jan 2, 2019):
It's a pity that it's an expected behaviour. From my point (as a doctrine user), it's a bug because the paginator allows custom fields in the
select / group byclause but doesn'tcountthem properly.if you restrict custom fields at all it'd be a BC.
I'd fix this behavior this way:
If only one field presents at the same time in
selectandgroup byclauses - then use this field as an identifier, otherwise, takegetSingleIdentifierFieldName()I could be wrong
@Ocramius commented on GitHub (Jan 2, 2019):
I'd rather say that the current behavior is undefined due to a missing restriction.
Nothing tells us that it is an identifier, hence it is not possible to assume that.
@azovsky commented on GitHub (Sep 13, 2022):
I have the same issue as described above: group by FIELD, but the result counts as without any grouping.
@azovsky commented on GitHub (Sep 13, 2022):
The next "fix" is working for me:
in file 'Doctrine/ORM/Tools/Pagination/CountWalker.php > function walkSelectStatement()':
@kriskoch commented on GitHub (Nov 22, 2022):
For me all I do is $paginator->setUseOutputWalkers(null) and it works fine. If you look at the code if it is set to null it will basically wrap your entire query in a select count(*) from (your query)