Hi All,
I have an Action model. Each Action belongs to a Person and an ActionCategory. The relations for these are defined as follows:
models/ActionCategories.php:
class ActionCategories extends CActiveRecord
{
.......
public function relations()
{
// NOTE: you may need to adjust the relation name ann the related
// class name for the relations automatically generated below.
return array(
'actionsRel' => array(self::HAS_MANY, 'Actions', 'category'),
);
}
models/Actions.php:
class Actions extends CActiveRecord
{
.......
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'actionCategoryRel' => array(self::BELONGS_TO, 'ActionCategory', 'id'),
'actionAssignedToRel' => array(self::BELONGS_TO, 'Person', 'id', 'together' => true),
);
}
models/Person.php:
class Person extends CActiveRecord
{
.......
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'actionsRel' => array(self::HAS_MANY, 'Actions', 'assigned_to'),
);
}
I want to use a CListView, so in my controller I instantiate a CActiveDataProvider and pass it to my view:
$dataProvider = new CActiveDataProvider(ActionCategory::model()->with('actionsRel'));
$this->render('index', array('actionCategories' => $dataProvider));
Finally, my view uses a CListView as follows:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$actionCategories,
'itemView'=>'_actionsView', // refers to the partial view named '_actionsView.php'
'sortableAttributes'=>array(
'name',
'id',
),
));
My partial view (_actionsView.php) can access
$data->actionRel
to obtain a list of Actions for each ActionCategory. However, I am unable to link each action to a person. That is, I cannot access
$data->actionRel[$i]->actionAssignedToRel->firstname
Please help. Thanks.