2

我有一个小问题。我创建了与公司和部门关系的用户 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'),
        );
    }

这是网格视图

<?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',
        ),
    ),
)); ?>

我该如何解决这个错误

4

2 回答 2

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'),

        ),
于 2012-07-30T09:50:14.130 回答
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'),
    ),
    ...
于 2012-07-31T07:33:30.367 回答