In Larry Ullman’s tutorial ‘Learning the Yii Framework’ he creates a small app with two tables Employee and Department.
On the ‘Manage Employee’ page there is a grid view where an admin user can filter Employee records. However there is only a ‘Department Id’ column, and so I added a ‘Department Name’ column for readability, as shown in the following code:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'employee-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'departmentId',
'department.name', //(I ADDED THIS LINE HERE.......!!!)
'firstName',
'lastName',
'email',
'ext',
/*
'hireDate',
'leaveDate',
*/
array(
'class'=>'CButtonColumn',
),
),
)); ?>
This works fine because of ‘relations’ method in the employee model
public function relations()
{
return array(
'department' => array(self::BELONGS_TO, 'Department', 'departmentId'),
);
}
However this column is neither sortable nor filterable in the same way that the other columns are.
Any clue how I would go about this?
And what is the general way of doing this sort of thing when tables are in relation like this?
Do I create an sql view in the database joining both tables and then run Gii on that… or is there a neater way of adapting the code I already have?