Sorting In Yii Cdbcriteria With Sum(Amount)

Sorting In Yii Cdbcriteria With highest Sum(Amount)on the top.

I have two models: members and billing, member_id is foregion key in billing model. My Model Function:

public function getImportantMembers(){       

    $criteria = new CDbCriteria;        

    $criteria->select ='t.*,b.billing_id,b.amount,b.billing_date,b.member_id,b.billing_status , sum(b.amount) AS totalamount';      

    $criteria->join = 'JOIN billing AS b ON b.member_id = ';

    $criteria->addCondition("b.billing_date > DATE_SUB(NOW(),INTERVAL 2 MONTH)  AND b.billing_status='c' AND b.amount > 150 ");               

    $criteria->group  = '';        

    $criteria->order = " totalamount DESC";                   

    return new CActiveDataProvider(get_class($this),array(


            'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),),          




but this function not showing correctly highest amount at the top or amount DESC How I can fix this?

Hi i think you can try the CArrayDataProvider like

return $arrayDataProvider = new CArrayDataProvider($criteria, array('id' => 'id',


            'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize'])



i am not sure it’s work or not

What is this id,where it comes from?

array('id' => 'id',

it’s optional if you want not show just remove id

this is created id on pagination id like