whiteoctober/Pagerfanta

Wrong results with groupBy, DoctrineORMAdapter, useOutPutWalkers must be true

evesinger opened this issue · 2 comments

Hello, I have a query that includes a group by and create the paginator with DoctrineORM like this:
$paginator = new Pagerfanta(new DoctrineORMAdapter($queryBuilder, true, false));
My expected count of results is 1 in this case and if I go to the queryBuilder and call getQuery->getResult it gives me the expected one. However, paginator->getNbResults() returns me 2.
Now, if I change the option "useOutPutWalkers" to true, ie. like this:
new Pagerfanta(new DoctrineORMAdapter($queryBuilder, true, true));, "count()" (or getNbResults()) returns the expected 1. I would like to know why this is, if it's a known issue or if it's expected behaviour. Any hints are welcome. Thanks very much.

stof commented

This is logical. Some cases simply cannot return the right result when forbidding Doctrine to use output walkers. And GROUP BY is such a case as putting the COUNT directly in the select would return the count in each group, not the number of groups.

If you don't understand how the Doctrine ORM Paginator class works, the best is to avoid configuring it explicitly. The default value for useOutPutWalkers is null, which lets Doctrine guess the strategy it will use.
And while the selected strategy may not be the most efficient for all queries as it will tend to use the output walker more often than strictly necessary, it is meant to give you the right result.

thanks for clarifying. Much appreciated.