Yii: Sorting CDbCriteria with custom variable in model

In my table there are fields like o_old_price (old price) and o_price (new price).

Is there any how I can calculate the discount from these prices and sort using it.

I had modified the model.

[i]public discount;

public function getDiscount()

{


$discount = (($this->o_old_price - $this->o_price)/$this->o_old_price) * 100; 


	return (is_null($discount)?$discount:0);


}

[/i]

I added this criteria in controller. But I dont think it is the right way.

[i]$criteria->order = 't.discount desc';[/i]

I think Yii will have its own ways to handle such situations. Please help me to do these things.

I think you can do this in your search method:




        return new CActiveDataProvider($this, array(

            'criteria' => $criteria,

            'sort' => array(

                'attributes' => array(

                    'discount' => array(

                        'asc' => '((o_old_price - o_price) / o_old_price)',

                        'desc' => '((o_old_price - o_price) / o_old_price) DESC',

                    ),

                    '*',

                ),

            ),

        ));



More optimized solution is to create view with the discount column.