j4mie/idiorm

Aggregate function support

j4mie opened this issue · 2 comments

From Sérgio Diniz (by email):

I found one thing that I really miss, is the max function, instead of using a raw query it could be already in your ORM… so I implemented it quickly:

/**
* Tell the ORM that you wish to execute a MAX query.
* Will return the max value of the choosen column.
*/
public function max($column)  {
   $this->select_expr('MAX('.$column.')', 'maxvalue');
   $result = $this->find_one();
   return ($result !== false && isset($result->maxvalue)) ? (int) $result->maxvalue : 0;
}

I only tested it in MySQL…

It looks like MAX, MIN, AVG and SUM are ANSI standard SQL so should be fairly portable. More research needed.

So far we only have count. Any chance we can have more of these fairly standard aggregation function?