Sort on a relational attribute (cgridview)

I have a CGridView that represents data from model Enquiry. I have the following column which is meant to display a relational attribute:





‘accepted_at’ is a field in model Quote, but in fact I can type whatever I want for ‘name’ and it just transforms it into the header text. The value is being displayed correctly.

I assume this is why the sort isn’t working for that column, because it isn’t referencing the actual field.

Here is the relation in the Enquiry model:

'accepted_quote'=>array(self::BELONGS_TO, 'Quote', 'accepted_quote_id'),

I also put in $criteria->with=array(‘accepted_quote’); in my search() function in the Enquiry model.


This is what I’ve done in my code to make this work:


'name' => 'accepted_at',


This is working for me, maybe you have to adjust to your particular case.

Also if you using the with keyword on the criteria that gives your dataProvider, you should use (in your case)


'name' => 'accepted_quote.accept_at'

It will use the correct value but it won’t be sortable, this requires something else that I quite haven’t understand myself on how to do it :)

But in the first example you’ll be able to sort by that column, maybe beeing a bit heavier on performance.

Hope it helps.

Sorry but that does not work either, in fact that does exactly the same thing as my code does, only difference is I use a function to call the Quote model and yours is making a call direct from the CGridView.

As I say the correct value is already being displayed, just the column isn’t sortable.

Anyone else able to advise?

BUMP. Anyone know how to make this column sortable please?

Check this thread -

I have tried doing it like this:


$sort=new CSort();

$sort->attributes = array('*', 'accepted_at');

return new CActiveDataProvider(get_class($this), array(




But I now get a javascript alert message saying:

"Column not found: 1054 Unknown column 't.accepted_at' in 'order clause'

BUMP. Anyone able to advise?

Have you checked the link I gave you before?

especially the post #3 -

Yes bro I checked it, as per my code above.

I specified the ‘with’ condition to load the relation

I created the CSort object and specified all attributes as well as the relational attribute

What else is missing?

But your sort attributes is not specified like that on the link… I will just copy/paste the sort attributes from that link…

  $sort->attributes = array(



      'desc'=>'customer.customer_name desc',




      'desc'=>'school.school_name desc',





‘customer’ and ‘school’ in this example are the column names for CGridView…

Thanks, this has worked for me:

$sort->attributes = array(



		'asc'=>'accepted_at', 'desc'=>'accepted_at DESC'