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 http://www.yiiframework.com/wiki/319/searching-and-sorting-by-count-of-related-items-in-cgridview/#hh4

My code is here -> http://pastebin.com/q7D2k7XG

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('company_id',$this->company_id);

		$criteria->compare('position',$this->position,true);

		$criteria->compare('email',$this->email,true);

		$criteria->compare('work_phone',$this->work_phone);

		$criteria->compare('cell_phone',$this->cell_phone);

		$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(

			'fullname'=>array(

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

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

			),

			'email'=>array(

				'asc'=>'email ASC',

				'desc'=>'email DESC',

			),

			'company_id'=>array(

				'asc'=>'name ASC',

				'desc'=>'name DESC',

			),

			'defaultOrder'=>array(

				'fullname'=>true,

				'email'=>false,

				'company_id'=>false,

			),

		);

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

		$criteria->together = true;

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

			'sort'=> $sort,

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

			'pagination'=>array(

				//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. http://pastebin.com/q7D2k7XG

I guess it’s working now with little change

I replaced ‘lastactivity’=>‘lastactivity’

with

    '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