这让我抓狂。我阅读了很多回复和教程,但我无法在此处指出问题所在。
我有 2 个表:tblEmpleado 和 tblProfesion Here is an image of the MySQL relations they have (Marked on red)
当显示来自 tblEmpleado 的数据时,我想在 CGridView
上显示来自表 tblProfesion 的属性“Descripcion”。
我尝试使用 claveProfesion.descripcion 但无济于事(它显示了 FK 的 INT 编号,没有“.descripcion”部分)。还尝试使用类似的东西:
array( 'header'=>'tableHeaderName', 'value'=>'(isset($data->claveProfesion)) ? $data->claveProfesion->descripcion : 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 View代码的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'),
);
}
关于php - YII CGridView。试图显示其他表的属性。关系不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17270221/