YII CGridView.正在尝试显示其他表中的属性.关系不起作用


YII CGridView. Trying to show attribute from other table. Relation not working

这让我抓狂。我读了很多回复和教程,但我无法在这里指出问题所在。

我有两个表:tblEmpleadoTBlProfion这是他们之间MySQL关系的图像(用红色标记)

当显示tblEmpiredo中的数据时,我想在CGridView上显示tblProfesion表中的属性"Description"。

我尝试过使用claveProfession.description,但没有成功(它显示了FK的INT编号,没有".description"部分)。也尝试过使用类似的东西:

阵列('header'=>'tableHeaderName','value'=>'(isset($data->clavePro))$data->claveProfesion->description:null',)

正在获取"正在尝试获取非对象的属性"。

以下是模型代码的Empleados.php关系部分:

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(
        'tblcelulars' => array(self::HAS_MANY, 'Tblcelular', 'claveEmpleado'),
        'tblcuentabancos' => array(self::HAS_MANY, 'Tblcuentabanco', 'claveEmpleado'),
        'idCentroTrabajo' => array(self::BELONGS_TO, 'Tblcentrotrabajo', 'idCentroTrabajo'),
        'convenio' => array(self::BELONGS_TO, 'Tblconvenio', 'convenio'),
        'claveProfesion' => array(self::BELONGS_TO, 'Tblprofesion', 'claveProfesion'),
    );
}

和Admin.php的CGridView部分查看代码

    <?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'empleados-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'claveEmpleado',
        'nombreEmpleado',
        'idCentroTrabajo',
        array(
                'header'=>'tableHeaderName',
                'value'=>'($data->claveProfesion!=null) ? $data->claveProfesion->descripcion : null',
        ),
        'aniosExperiencia',
        'telefono2',
        'email',
...
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

最后,我不知道它是否相关,但在模型类"Profesiones.php"中,关系如下:

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(
        'tblempleados' => array(self::HAS_MANY, 'Tblempleado', 'claveProfesion'),
    );
}

有人能帮我吗?

您的列和关系都具有相同的名称claveProfesion。因此,当您调用claveProfesion时,Yii将返回列而不是关系。要解决此问题,请将您的关系重命名为其他内容,例如claveProfesionObject

public function relations()
{
    return array(
        ...
        'claveProfesionObject' => array(self::BELONGS_TO, 'Tblprofesion', 'claveProfesion'),
    );
}