gbvisconti
gbvisconti

Reputation: 412

Yii CGridView data is foreign key

I'm having some hard time with CGridView, where one field is the foreign key to another table.

There is a table called Person which contains an id_scholarity

And a table Scholarity where id_scholarity is PK. I want to show the description of scholarity and not the id number.

Gii has created the relations:

In Scholarity model:

return array(
            'person' => array(self::HAS_MANY, 'PERSON', 'ID_SCHOLARITY'),
        );

In Person Model

return array(
    'id_scholarity' => array(self::BELONGS_TO, 'SCHOLARITY', 'ID_SCHOLARITY'),
);

And finally my grid (in views/person/admin.php)

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'person-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(

        'NAME',
        array('name'=>'ID_SCHOLARITY', 'value'=>'$data->ID_SCHOLARITY->DESCRIPTION'),

        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); 

The page just gets blank (by the way, how can I make yii show errors?). What I'm doing wrong?

Upvotes: 0

Views: 431

Answers (1)

Rafay Zia Mir
Rafay Zia Mir

Reputation: 2116

try

'columns'=>array(

        'NAME',
        array('value'=>'$data->id_scholarity->DESCRIPTION'),

        array(
            'class'=>'CButtonColumn',
        ),
    ),

when you access other table with arrow operator you have to access it using relation name not the attribute name. In your code relation name is id_scholarity but you are using ID_SCHOLARITY .

Upvotes: 1

Related Questions