. */ namespace App\DataTables\Adapters; use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\Tools\Pagination\Paginator; use Omines\DataTablesBundle\Adapter\Doctrine\FetchJoinORMAdapter; /** * This class is a workaround for a bug (or edge case behavior) in the FetchJoinORMAdapter or better the used Paginator * and CountOutputWalker. * If the query contains multiple GROUP BY clauses, the result of the count query is wrong, as some lines are counted * multiple times. This is because the CountOutputWalker does not use DISTINCT in the count query, if it contains a GROUP BY. * * We work around this by removing the GROUP BY clause from the query, and only adding the first root alias as GROUP BY (the part table). * This way we get the correct count, without breaking the query (we need a GROUP BY for the HAVING clauses). * * As a side effect this also seems to improve the performance of the count query a bit (which makes up a lot of the total query time). */ class CustomFetchJoinORMAdapter extends FetchJoinORMAdapter { public function getCount(QueryBuilder $queryBuilder, $identifier): int { $qb_without_group_by = clone $queryBuilder; //Remove the groupBy clause from the query for the count //And add the root alias as group by, so we can use HAVING clauses $qb_without_group_by->resetDQLPart('groupBy'); $qb_without_group_by->addGroupBy($queryBuilder->getRootAliases()[0]); $paginator = new Paginator($qb_without_group_by); return $paginator->count(); } }