Rahil
Rahil

Reputation: 107

reference table value not displayed in Yii GridView

I have an problem to display reference table value in cGridView.

Here is the relation. each user have multiple images and each image has single user.

UserImages.php model

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(
        'user'=>array(self::BELONGS_TO,'Users','user_id')
    );
}

Users.php Model

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(
            'images'=>array(self::HAS_MANY,'UserImages','user_id')
        );
}

User image grid view

$this->widget('zii.widgets.grid.CGridView', array(

'id'=>'user-images-grid',

'dataProvider'=>$model->search(),

'filter'=>$model,

'columns'=>array(

    'image_id',

    'user_id',

    array('name'=>'username', 'value'=>'$data->user->username', 'header'=>'User Name'),

    array('name'=>'filename','type'=>'raw','value'=>array($this,'gridThumb')),


    'caption',

    array(
        'class'=>'CButtonColumn',
        'template'=>'{update} | {delete}',
        'updateButtonImageUrl'=>false,
        'deleteButtonImageUrl'=>false,          
    ),
),

));

But every time i got this error: Property "UserImages.username" is not defined.

Is there anything wrong i am doing ? please help.

Note : both table contains user_id column

Upvotes: 1

Views: 301

Answers (1)

Manquer
Manquer

Reputation: 7647

You do not need both name and value in attribute. Change

 array('name'=>'username', 'value'=>'$data->user->username', 'header'=>'User Name'),

to

array('value'=>'isset($data->user->username)?$data->user->username:""', 'header'=>'User Name'),

it should solve your problem

See http://www.yiiframework.com/doc/api/1.1/CDataColumn#name-detail for more details

Upvotes: 1

Related Questions