CGridView and sortable columns from referenced tables

I use a CDataProvider and a CGridView to display a listing of a model’s records. In CDataProvider, I have this code:

$dataProvider = new CActiveDataProvider('Receipt', array(

'criteria'=>array('with'=>array('category', 'theBuyer', 'persons')),


Receipt belongs to many categories (category relation). The grid is displayed nicely:

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


'columns'=>array('id', 'afm', 'date', 'price', 'reason',

array('name'=>'Category name', 'value'=>'$data->category->name', 'sortable'=>TRUE),




but all columns are sortable except from the category name column (from the category relation). How do I make it sortable?

Ok, the answer is in the Yii 1.1 Application Development Cookbook (page 195). I have to use a CSort object and pass this object to the sort attribute of the CActiveDataProvider object. In my case, the code is:

$sort = new CSort;

$sort->attributes = array('Category name'=>array('asc'=>'name', 'desc'=>'name DESC'), '*');

$sort->defaultOrder = array('date'=>TRUE);

$dataProvider = new CActiveDataProvider('Receipt', array(




Then, the Category name column is rendered sortable.