0

I have a complex query which group bys multiple columns, this is an example

$queryBuilder
    ->join('e.something', 's')
    ->where('...')
    ->addGroupBy('e.id s.someField');

Using ->getQuery()->getResults() returns me an array of entities like I'd expect.

[
    0 => App\Entity\ExampleEntity,
    1 => App\Entity\ExampleEntity
]

If I try to count the results returned using a select like so:

$queryBuilder
    ->select('COUNT(e.id)')
    ->join('e.something', 's')
    ->where('...')
    ->addGroupBy('e.id s.someField');

I am returned an array of arrays, inside each array is the count. This isn't what I want. Removing the group by I'm given the correct result however the group by is required.

[
    [
        1 => '11',
    ],
    [
        1 => '4',
    ]
]

I'm stuck on how I can count the results of the group by. I have tried using distinct and I also do not want to count in PHP.

MylesK
  • 3,349
  • 2
  • 9
  • 31
  • I don't really understand, if you add a group by, it's quite normal the count uses the group by ! What's the logic behind adding a group by to not use it in the count ? – Alexandre Oct 07 '20 at 08:52
  • Not saying it's not normal I just don't know how to get around it. I only shared a simplified version but the query is used for a list page which filters and sorts – MylesK Oct 07 '20 at 09:02
  • Dupplicate https://stackoverflow.com/questions/43202217/get-count-query-results-via-getsinglescalarresult-and-groupby-exception – vincent PHILIPPE Oct 07 '20 at 12:10
  • Does this answer your question? [Get count query results via getSingleScalarResult() and groupBy() - exception](https://stackoverflow.com/questions/43202217/get-count-query-results-via-getsinglescalarresult-and-groupby-exception) – vincent PHILIPPE Oct 07 '20 at 12:10

0 Answers0