Sum/aggregation With Group By In A Model

Hi all,

Im having problem with aggregation in yii. well i have a product_sold table with product_id and quantity in its field.

I wanna get the sum of all products with their total quantity sold.

for example here’s the data on product_sold

product | quantity

product1 | 3

product1 | 2

product2 | 7

product2 | 7

i want the resulting table to be:

product | quantity

product1 | 5

product2 | 14

so i created a method in my product sold model like this:


         * Retrives the summary of products


        public function getSummary(){

            $criteria=new CDbCriteria;

            $criteria->group = "product_id";

            $criteria->select = "*,sum(quantity) as sum_quantity";

            return new CActiveDataProvider($this, array(




the problem is i cant access the sum_quantity field in the CListView by using $data->sum_quantity. am i missing something?

or should i create a property in my model with $sum_quantity as its name?

also i saw this post using STAT as a relation but i couldnt grasp how to do it since the parameter for relations method is

'varName'=>array('relationType', 'className', 'foreignKey', ...additional options)

but i dont have a foreignKey to pass to the third param since quantity field is in the same table.