Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Calculate Sum For Cactiverecord Column

$
0
0
Hi,

I'm struggling with the following problem:

I have a page showing a list of active records (using CActiveDataProvider). CActiveDataProvider provides a method to calculate the total number of rows (calculateTotalItemCount()). Now I want to create a similar method to calculate the sum for a certain column. I'm having a hard time thinking of an elegant way...

My search() method creates table joins so the CActiveFinder class is used to retrieve records en count the total amount of records.


So far I tried the following, in my active record class I added a method:

public function calculateSum() {
	// Duplicate the original CDbCriteria object (created in search())
	$criteria = clone $this->_criteria;
	// Replace the 'select' part by 'sum'
	$criteria->select = 'SUM(amount) AS total';
	$count = $this->count($criteria);
	$this->setDbCriteria($this->_criteria);
	return $count;
}


Unfortunately this doesn't work since the select part is replaced in CJoinElement.count() resulting in a regular count (instead of sum).

Thanks in advance for any help!

Viewing all articles
Browse latest Browse all 18717

Trending Articles