Yii CGridView DataProvider

时间:2013-06-18 10:28:27

标签: php yii

我有两个模型:项目和用户。在项目中:

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(
            "users"=>array(self::MANY_MANY, 'User',
            'projects_users(project_id, user_id)'),
    );
}

我想列出实际项目中的所有用户,$ model包含实际项目:

    <?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'user-grid',
    'dataProvider'=>$model->users,
    'columns'=>array(
        'ID',
        'username',
        'displayname',
        'firstname',
        'lastname',
        'email',
        /*
        'password',
        'isAdmin',
        */

        array(
            'class'=>'CButtonColumn',
            'template'=>'{delete}',
        ),
    ),
)); ?>

不幸的是我收到了一个错误:

Fatal error: Call to a member function getData() on a non-object in /var/www/vhosts/aevers.com/editor/framework/zii/widgets/CBaseListView.php on line 107

2 个答案:

答案 0 :(得分:5)

那是因为 $ model-&gt;用户不是 CActiveDataProvider :它是CActiveRecords的数组

尝试使用CArrayDataProvider代替:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'user-grid',
    'dataProvider'=> new CArrayDataProvider($model->users),
    [...] 
    ),
)); ?>

答案 1 :(得分:0)

U可以根据cgridView中的项目和用户进行分组和显示结果 试试这个帖子Grouping members