Sorting relations

Hello, I have the following:

$items = $model->B->C;  //where B,C are relations - basically just a relation of a relation

Anyways, I just want to know how to sort the values in $items. I went to the model file in C and added the following into the search method’s dataprovider return value:


	'defaultOrder'=>'attribute X', //tried ASC and DESC


but that didn’t change the order at all. Any help is appreciated, thanks.

Just had the same problem, if I understand what you are asking, you need to include them in a criteria like this

$criteria=new CDbCriteria(array(



Then put the criteria in the data provider

That didn’t seem to work. Just to clarify, B is a relation from another model, and C is a relation from yet another model. So $model->(relation between A and B )->(relation between B and C)

If you want to have the relation start with a certain order, then you need to set the ‘order’ option in the relations() method in your AR model class.

'varName'=>array('relationType', 'className', 'foreign_key', 'order'=>'column ASC')

1 Like

it worked, thanks!

haaaa… You save my life… Thanks