October 2009
1 post
Use MySQL's SUM function with CakePHP find()
Saw this on IRC earlier, thought I’d blog it as it’s pretty useful info.
Add this method to your AppModel:
public function sum($field='total')
{
$data = $this->find('first', array(
'conditions'=>$conditions,
'fields'=>array('SUM('.$field.') AS summed'),
'contain'=>array()
));
return $data[0]['summed'];
}
Then call it like so:
$total =...