Yii GridView中未显示参考表值

Yii GridView中未显示参考表值,gridview,yii,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(

我在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”。

我做错什么了吗?请帮忙


注意:这两个表都包含用户id列

属性中不需要名称和值。改变

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

它应该能解决你的问题


有关更多详细信息,请参见

谢谢,但它不起作用。现在我遇到了这个错误:如果有任何帮助,我将不胜感激?这意味着您有一些没有用户id的行,因此未设置$data->user object,因此非对象的属性您可以如上所述修改值以解决问题:如何打印GridView中显示的所有行数组?$model->search()返回CActiveDataProvider您可以通过数据提供程序循环,更简单的方法是检入phpmyadmin或类似工具。如果您使用我在上面更改的方法检查对象是否存在,它将正常工作。非常感谢,您上次的修复工作正常。没错,有些记录不包含与用户相关的值。再次感谢
array('value'=>'isset($data->user->username)?$data->user->username:""', 'header'=>'User Name'),