Sort Cgridview By Custom Field


I’m aware that similar questions have been asked, sorry, but I can’t for the life of me see what I’m doing wrong. Any help would be greatly appreciated. All I want to do is to add an extra column to a CGridView that displays a total of two values in the database.

These are the steps I’ve taken:


User(forename, surname, part1, part2)


class User extends Controller:

	protected function getTotal($data,$row,$dataColumn){

		return $data->getAttribute('part1')+$data->getAttribute('part2');


	public function actionIndex(){

		$dataProvider=new CActiveDataProvider('User');





class User extends CActiveRecord:

	public function rules(){

		return array(

			array('forename, surname', 'required'),

			array('forename', 'surname', 'max'=>50),

			array('part1, part2', 'numerical', 'integerOnly'=>true),

			array('forename, surname, part1, part2, total', 'safe', 'on'=>'search'),



	public function attributeLabels()


		return array(

			'forename' => 'forename',

			'surname' => 'surname',

			'part1' => 'part1',

			'part2' => 'part2',

			'total' => array(

		                'asc'=>'part1 + part2',

                		'desc'=>'part1 + part2 desc',




	public function search(){

		$criteria=new CDbCriteria;





		$criteria->compare('total',$this->part1 + $this->part2,true);

		$sort = new CSort();

		$sort->attributes = array(


		        'asc'=>'part1 + part2 ASC',

		        'desc'=>'part1 + part2 DESC',




		return new CActiveDataProvider(User', array(







  	<?php $this->widget('zii.widgets.grid.CGridView', array(

			'itemsCssClass'=>'table table-hover',




				array('name'=>'forename', 'header'=>'Forename'),

				array('name'=>'surname', 'header'=>'Surname'),

				array('name'=>'total', 'header'=>'Total', 'value'=>array($this, 'getTotal')),


		)); ?>

<?php $this->endWidget();?>

Thanks a lot for any advice.