Skip to content

Commit

Permalink
Make computeNbResult() compatible with GROUP BY
Browse files Browse the repository at this point in the history
Using getOneOrNullResult() causes an exception in case of a query with
GROUP BY. To fix the issue, we allow multiple rows and do a sum over the
cnt column, which holds the count for each group.

Fixes sonata-project#968
  • Loading branch information
VincentLanglet authored and OskarStark committed Feb 4, 2020
1 parent ffa2275 commit 367a7c9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/Datagrid/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ public function computeNbResult()
current($this->getCountColumn())
));

return (int) ($countQuery->resetDQLPart('orderBy')->getQuery()->getOneOrNullResult(Query::HYDRATE_SINGLE_SCALAR));
return array_sum(array_column(
$countQuery->resetDQLPart('orderBy')->getQuery()->getResult(Query::HYDRATE_SCALAR),
'cnt'
));
}

public function getResults($hydrationMode = Query::HYDRATE_OBJECT)
Expand Down
5 changes: 3 additions & 2 deletions tests/Datagrid/PagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ public function testComputeNbResult($distinct): void
{
$query = $this->getMockBuilder(AbstractQuery::class)
->disableOriginalConstructor()
->setMethods(['getOneOrNullResult'])
->setMethods(['getResult'])
->getMockForAbstractClass();

$query->expects($this->once())
->method('getOneOrNullResult');
->method('getResult')
->willReturn([['cnt' => 1], ['cnt' => 2]]);

$queryBuilder = $this->getMockBuilder(QueryBuilder::class)
->disableOriginalConstructor()
Expand Down

0 comments on commit 367a7c9

Please sign in to comment.