Gridview : order problem on foreign key name

Hello,

I just discovered that my working yii application has this problem :

I have Planningledger table which has a "Belong to" relation with the table Employee

Problem is that I display Planningledger gridview and I have it ordered correctly by Emmployee Name

But when I’m clicking on the column header to order by Employee name it orders by the Employee id (foreign key)

Here’s what I have in Planningledger model :

function rules :


array('employee.fullname, id, date, employee_id, description, activity_id, solde', 'safe', 'on'=>'search'),

function relations :


'employee' => array(self::BELONGS_TO, 'Employee', 'employee_id'),

function search() :




$criteria = new CDbCriteria;

$criteria->with = array( 'activity','employee' );

$criteria->compare('id',$this->id);

$criteria->compare('date',$this->date,true);

$criteria->compare('description',$this->description,true);

$criteria->compare('activity_id',$this->activity_id);

$criteria->compare('solde',$this->solde);

$criteria->compare('create_time',$this->create_time,true);

$criteria->compare('create_user_id',$this->create_user_id);

$criteria->compare('update_time',$this->update_time,true);

$criteria->compare('update_user_id',$this->update_user_id);

$criteria->compare('employee.fullname',$this->employee->fullname, true);

$criteria->together = true;

$sort = new CSort();

$sort->attributes = array(

	'employee_id'=>array(

		'asc'=>'employee.fullname',

		'desc'=>'employee.fullname desc',

	),

	'activity_id'=>array(

		'asc'=>'activity.name',

		'desc'=>'activity.name desc',

	),

);

return new CActiveDataProvider($this, array(

	'pagination'=>array(

		'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),

	),

	'criteria'=>$criteria,

	'sort'=>$sort,

	'sort'=>array(

		'defaultOrder'=>'employee.fullname ASC',

	)

));



And this is part of my view :




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

	'id'=>'planningledger-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'employee_id'=>array(

			'header' => Yii::t('app','model.planningledger.employee'),

			'name'=>'employee_id',

			'value'=>'($data->employee_id === null) ? \' \' : $data->employee->fullname',

			'filter'=>CHtml::listData(Employee::model()->findAll(array('condition'=>'enabled=true', 'order'=>'fullname ASC')), 'id', 'fullname'),

        ),



All the other stuff (dropdownlist, searching, sorting on date) works fine.

But as I’m not displaying the id but the name I really must find the mistake I made.

I already read many posts but I can’t get it work.

You can not add ‘sort’ twice to the array, you should only add it once.

The second definition of ‘sort’=> overrides the previous one.

Further, I recommend the use of my "RelatedSearchBehavior" Extension: http://www.yiiframework.com/extension/relatedsearchbehavior/ .

You can define ‘fullname’ as a “virtual” related attribute and sorting will be taken care of automatically. ‘CGridView’ will not really notice that it is a virtual attribute.

Check out the demo of the extension!

Oups - I did not see THIS - thanks a lot !

I will have a look too to your extension …