ar: order by stat relation


Model relations:

public function relations()


	return array(

		'lastReplyTime' => array(


			'select' => 'MAX(created)',




Model search:

public function search()


	$criteria=new CDbCriteria;		

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

	return new CActiveDataProvider(__CLASS__, array(


		'sort' => array(

			'defaultOrder' => 'lastReplyTime DESC',




Returns error.

Actually even if I remove the sorting stuff it doesn’t load lastReplyTime eagerly - it still creates a separate query for it. Is there any solution for this?

Thank you.

I usually never use the STAT relationship, I simply edit the select statement, is more simple:

$criteria= new CDbCriteria();

$criteria->select= "t.*, MAX(created) as max";

You should also add a property max in the model for collect this field:

class [] extends CActiveRecord


   public $max;