参考表值未在Yii GridView中显示

时间:2014-06-10 08:03:32

标签: gridview yii

我在cGridView中显示引用表值时遇到问题。

这是关系。每个用户都有多个图像,每个图像都有单个用户。

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

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

用户图片网格视图

$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,          
    ),
),

));

但每次我收到此错误: 属性" UserImages.username"未定义。

我在做什么事吗?请帮忙。

注意:两个表都包含user_id列

1 个答案:

答案 0 :(得分:1)

您不需要属性中的名称和值。变化

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

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

它应该解决你的问题

有关详细信息,请参阅http://www.yiiframework.com/doc/api/1.1/CDataColumn#name-detail