Newbie Question: How to best sort with CListView using relations

I can’t find any examples on the site showing how to use CListView to sort using the value of a related field?

For example, if my table is storing a user id, but I wish to use CListView to sort by the user name in the user table, how can I best implement this?

I’ve tried


<?php $this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

        'sortableAttributes'=>array(

        'user_id.name',

    ),


)); ?>

and a number of other shot in the dark approaches, but obviously I am missing something very fundamental. Can anyone here point me in the right direction?

Thanks so much.

If you have the relation setted up, you can change the dataProvider like that:




$dataProvider->criteria->with('User'); //that works if you have the relation with users

$dataProvider->sort=array(

     'user.name', ... //all other fields

);



Using "with" CActiveRecord will fetch even the related table, so you can sort for field in user.

as far as I know, CDbCriteria do not have ‘with’ method, so this maybe throw an error.

maybe this will work:


$criteria->with = array(

	'user'=>array('select'=>'username'),

);

// then you have to declare in sort

return new CActiveDataProvider('Category', array(

	'criteria'=>$criteria,

	'sort'=>array(

		'attributes'=>array(

			'username'=>array(

				'asc'=>'username',

				'desc'=>'username DESC',

			),

// don't forget re-define the columns of 'Category' table bc it will be overwritten