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?