Using 'alias' property with relational data

I’m using Yii 1.1.3.

I have table Supplier:

id (int)

name (varchar)

town_id (int)

email (varchar)

phone (varchar)

enabled (int)

I also have table Town:

id (int)

name (varchar)

region_id (int)

I have a join from Supplier to Town, so that I can retrieve the region_id of the Supplier.

I represent this data in a CGridView as follows:


public function relations()

{

	return array(

		'supplier_town'=>array(self::BELONGS_TO, 'Town', 'town_id'),

	);

}


public function search()

{

	$criteria=new CDbCriteria;

	$criteria->alias="supplier";

	$criteria->with=array('supplier_town');


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

		'criteria'=>$criteria,

		'sort'=>array(

			'defaultOrder'=>'supplier.name',

			'attributes'=>array(

				'region'=>array(

					'asc'=>'region',

					'desc'=>'region DESC',

				),

				'*',

			)

		),

	));

}

I use the ‘alias’ property as both the tables contain a ‘name’ field.

As you can see I have specified an ‘attributes’ element in the sort array so that the relational field ‘region’ can be sorted on. I have also included the ‘*’ element in the attributes array - according to the docs this ensures that all other model attributes are available for sorting. So far so good, however when I try to sort on any of the other columns I receive a JavaScript alert error, for example:

Column not found: 1054 Unknown column ‘t.enabled’ in ‘order clause’

I found that if I remove the alias property the error does not occur, but then of course I get an ambiguous ‘name’ column.

The only way I can get this to work with the alias is by taking out the star element and creating seperate elements for all the other attributes. This is a workaround but I was wondering why the ‘alias’ property is causing this problem?

I had this problem too, but always re-wrote all attributes.

Maybe you can ask as feature request CSort will take in accont the eventual alias of the criteria, but I am not sure it is possible.

Yeh I think there should be an ‘alias’ property for CSort!

+1 for an alias attribute for CSort.

Is there any other way when using an alias?

This issue is fixed in r2582 - http://code.google.com/p/yii/source/detail?r=2582

brillant thanks guys