문제

This is driving me nuts. I read many responses and tutorials and i can't pin point the problem here.

I have 2 Tables: tblEmpleado and tblProfesion Here is an image of the MySQL relations they have (Marked on red)

I want to display on a CGridView the attribute "Descripcion" from the table tblProfesion when displaying the data from tblEmpleado.

I tried using claveProfesion.descripcion without avail (It shows the INT number of the FK just fine without the ".descripcion" part). Also tried using something like:

array( 'header'=>'tableHeaderName', 'value'=>'(isset($data->claveProfesion)) ? $data->claveProfesion->descripcion : null', )

getting "Trying to get property of non-object".

Here is the Empleados.php relation part of the model code:

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

And the CGridView part of the Admin.php View code

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

And last, i don't know if its related but in the Model class "Profesiones.php" the relation is as follow:

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

Could anyone help me out with this?

도움이 되었습니까?

해결책

Your column and your relation both have the same name claveProfesion. As such Yii is returning the column instead of the relation when you call claveProfesion. To resolve it rename your relation to something else e.g claveProfesionObject

public function relations()
{
    return array(
        ...
        'claveProfesionObject' => array(self::BELONGS_TO, 'Tblprofesion', 'claveProfesion'),
    );
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top