How to sort by related attribute?

I have 2 sql tables,

Products, Products_Descriptions

Products_Descriptions has "id, prodId, name, desc, langId"

I want to sort by "name".

Here is my controller code…




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

	'criteria'=>array(

		'select' => 'imageId, id, price, wholesale_price',

		'with'=>array(

			'descriptions'=>array(

				'select' => 'name',

				'condition'=>'languageId='.$this->languageId,

			),

		),

	),

	'sort' => array(

		'defaultOrder' => 'date DESC',

		'separators' => array(

			',',

			'-'

		),

		'attributes' => array(

			'name' => array(

				'asc' => 'descriptions.name ASC',

				'desc' => 'descriptions.name DESC',

				'label' => 'qwe',

			),

			'*',

		),

	),

));


$this->render('list', array(

	'dataProvider' => $dataProvider,

));



and here is my view code




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

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

	'ajaxUpdate' => false,

	'sortableAttributes'=>array(

		'price',

		'wholesale_price',

		'date',

		'name',

	),

));



but when i hit the "Name" sort. Error occured.

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘descriptions.name’ in ‘order clause’. The SQL statement executed was: SELECT t.imageId AS t0_c1, t.id AS t0_c0, t.price AS t0_c4, t.wholesale_price AS t0_c5 FROM prod_sub1_products t ORDER BY descriptions.name ASC LIMIT 10

i really want to know how to sort by related attribute

query in error message shows that there is no join with the other table… what is the relation type of ‘descriptions’? probably HAS_MANY? if so - try to add ‘together’=>true to ‘with’=>array( ‘descriptions’=>array( [here] ) ) so Yii will join tables in one query instead of merge objects from two separate queries

its great, thank you