Sorting Issue With Computational Field

I have issues with sorting of the field that are computed ie max(date).

I want to display the data and keep it sortable. I am using CDbCriteria and CActiveDataProvider to display it in CGridView.

my computation field name is lastactivity. I have added it as safe under rules as well… I tried to achive as mentioned in this reference link

My code is here ->

I have also mentioned error that I get in the link.

Is that sample model your Contact model, and are you trying to generate a list of all contacts with their most recent action (stored inside the ContactAction model)?

don’t know what you’re trying to do but this this is what i do for columns that aren’t in my db. maybe it will help you or someone else.

	public $fullname;

	public function getFullName(){

		return $this->first_name.' '.$this->last_name;


	public function search()


		$criteria=new CDbCriteria;






		$criteria->compare('CONCAT(first_name, " ", last_name)',$this->fullname,true);

		//we have to manually set this manually because we need to be able to sort grid with virtal attributes and by related field name vs id.

		$sort = new CSort();

		$sort->attributes = array(


				'asc'=>'last_name ASC, first_name ASC',

				'desc'=>'last_name DESC, first_name DESC',



				'asc'=>'email ASC',

				'desc'=>'email DESC',



				'asc'=>'name ASC',

				'desc'=>'name DESC',








		$criteria->with = array('company');

		$criteria->together = true;

		return new CActiveDataProvider($this, array(


			'sort'=> $sort,

			//sets the page size whenever we use $model->search() as the data provider i.e. in contract/index.php


				//gets default page size from config.php params

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




Yes, that’s exactly I am trying…Contact and Contact action are having Parent-Child relationship…I want to display records of Contact order by max(date) for Contact actions…so that will give me records of Contact on which most resent comments are done.

I have extended CDataColumn with custom class gridDataColumn but when I used CSqlDataProvider instead of CActiveDataProvider it throws errors which I suppose is due to one returning object and another plain array. So i want to go with the cdbcriteria based solution…

I have HAS_MANY relationship but I want it to work for computational field which will be max(date)…but still error persists as in pastebin.

I guess it’s working now with little change

I replaced ‘lastactivity’=>‘lastactivity’


    'lastactivity' => array(

            'asc' => 'lastactivity ASC',

            'desc' => 'lastactivity DESC',


Now, I am curious why the change is not throwing below error anymore.

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘t.lastactivity’ in ‘order clause’. The SQL statement executed was: SELECT *, (select max(date) from tbl_contact_action ca where ca.contact_id=t.contact_id) as lastactivity FROM tbl_contact t WHERE status<>“Deleted” ORDER BY lastactivity DESC, t.lastactivity DESC LIMIT 10