我有一个小问题。我创建了用户CGridView,它与公司和部门有关系。
错误:公司名称显示在网格视图中。但是部门名称不会显示在gridview中。
当我取消注释'value'=> .....,“试图获取非对象属性”时显示。
此模型关系
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(
'ranks' => array(self::BELONGS_TO, 'Rank', 'rank_id'),
'companies' => array(self::BELONGS_TO, 'Company', 'company_id'),
'departments' => array(self::BELONGS_TO, 'Department', 'department_id'),
'departmentsdep' => array(self::BELONGS_TO, 'Department', 'department_id'),
'departmentssec' => array(self::BELONGS_TO, 'Department', 'section_id'),
'departmentstea' => array(self::BELONGS_TO, 'Department', 'team_id'),
);
}
这是gridview
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'login_name',
'first_name',
'last_name',
'email',
array(
'name' => 'company_id',
'value'=>'$data->companies->name',
'filter'=>CHtml::listData($companylist, 'id', 'name'),
),
array(
'name' => 'department_id',
//'value'=>'$data->departmentsdep->name',
'filter' =>
CHtml::listData(
is_numeric($model->company_id) ? Department::model()->findAll(new CDbCriteria(array(
'condition' => 'p_id = 0 AND company_id=:company_id',
'params' => array(':company_id' => $model->company_id),
))) : $department, 'id', 'name'),
),
array(
'name' => 'section_id',
//'value'=>'$data->departmentssec->name',
'filter' =>
CHtml::listData(
is_numeric($model->department_id) ? Department::model()->findAll(new CDbCriteria(array(
'condition' => 'p_id = :pid AND company_id=:company_id',
'params' => array(':company_id' => $model->company_id, ':pid'=>$model->department_id),
))) : $department, 'id', 'name'),
),
array(
'name' => 'team_id',
//'value'=>'$data->departmentstea->name',
'filter' =>
CHtml::listData(
is_numeric($model->department_id) ? Department::model()->findAll(new CDbCriteria(array(
'condition' => 'p_id = :pid AND company_id=:company_id',
'params' => array(':company_id' => $model->company_id, ':pid'=>$model->section_id),
))) : $department, 'id', 'name'),
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
如何解决此错误
答案 0 :(得分:0)
这是我的解决方案
'value' => '($data->department_id == 0) ? "" : $data->departmentsdep->name',
完整代码
array(
'name' => 'department_id',
'value' => '($data->department_id == 0) ? "" : $data->departmentsdep->name',
'filter' =>
CHtml::listData(
is_numeric($model->company_id) ? Department::model()->findAll(new CDbCriteria(array(
'condition' => 'p_id = 0 AND company_id=:company_id',
'params' => array(':company_id' => $model->company_id),
))) : $department, 'id', 'name'),
),
答案 1 :(得分:0)
您可以使用'。'访问关系签署关系。这样会更容易:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'login_name',
'first_name',
'last_name',
'email',
array(
'name' => 'companies.name',
'filter'=>CHtml::listData($companylist, 'id', 'name'),
),
...