I have three tables: Department 1–>n employee n<–1 bargaining_unit
In my Department model, I declare the following relations:
public function relations()
{
return array(
'employees' => array(self::HAS_MANY, 'employee', 'department_id','with'=>'bargaining_unit'),
);
}
In employee, I have
public function relations()
{
return array(
'department' => array(self::BELONGS_TO, 'department', 'department_id'),
'bargaining_unit' => array(self::BELONGS_TO, 'bargaining_unit', 'bargaining_unit_id'),
);
}
And in my department controller, I have
$dataProvider=new CActiveDataProvider('Department', array(
'criteria'=>array(
'with'=>array('employees'), //I've also tried the below with employees.bargaining_unit
)
));
In my _view, I want to output all employees in a given department, together with the name of their bargaining units, sorted by bargaining unit (i.e. like this
The trouble is that I can’t figure out how to give the reference. I’ve looked up the examples in the tutorial and googled the forum and yiiframework.com site generally. And from that I would expect to add a line something like the fourth line in the code below to my dataprovider:
$dataProvider=new CActiveDataProvider('Department', array(
'criteria'=>array(
'with'=>array('employees'),
'order'=>'employees.bargaining_unit.name DESC',
)
));
This seems to have no effect when it is invoked in a department view. Nor do any of the following:
'order'=>'t.employees.bargaining_unit.name DESC',
'order'=>'bargaining_unit.name DESC',
'order'=>'name DESC',
Can anybody explain how you sort on a second child like this? I should say that I can sort by adding a ‘with’ statement to relations()
public function relations()
{
return array(
'employees' => array(self::HAS_MANY, 'employee', 'department_id','with'=>'bargaining_unit','order'=>'bargaining_unit.name'),
);
}
But what if I don’t want to set it as a default in the model or if I need to verride it somewhere else and use a different order?