XF 2.2 Fetching SUM(column) via the Finder

Zig

Member
Hello everyone,

I'm wondering, is it possible to use the Finder to fetch the SUM() of a certain column?

In my search for examples of SUM() in the core I've found only examples of raw SQL. I'd change course and go this route, but in this particular case I already have a Finder object with all the necessary where() and whereOr() clauses applied. I'd like to avoid refactoring if possible.

Best,
Zig
 

Lukas W.

Well-known member
Of the top of my head I think the answer to that is no. The finder system is meant to return entities to you, so aggregations don't really fit into that. There is the exception of the total() function though, which you could probably look into and build a similar function to fetch the aggregation of a column.
 
  • Like
Reactions: Zig

Zig

Member
Use of total() is actually why I wanted to keep the Finder object I already have built. I tried out fetchColumns('SUM(column)'), but that failed due to the Finder's internal checks (as it should). I hadn't thought about extending the Finder itself and might try that in the future. In this case though, I'll probably just refactor and use db() to get the job done.

Thanks for the response!
 
Top