我是Yii的新手,试图在主页上显示用户列表,但在这样做的时候我遇到了错误:属性"CListView.userlist"没有定义。代码:SiteController.php
class SiteController extends Controller {
// This is the function which I changed reset is file remains same
public function actionIndex() {
$userlist = User::model()->findAllBySql($sql);
$this->render('index',array('userlist'=>$userlist,));
}
}
网站视图的代码,index.php:
$this->widget('zii.widgets.CListView', array(
'userlist'=>$userlist,
'itemView'=>'_view',
));
在last_view.php中:
echo CHtml::encode($data->getAttributeLabel('id'));
当前在中,代码为:未定义属性"CListView.userlist"。但是,如果我用$dataProvider替换$userlist,错误将变为"致命错误:调用C:''devCenter''yii''framework''zii''widgets''CBaseListView.php中107行的非对象上的成员函数getData()"
我有两个问题:
- $dataProvider似乎有特殊的含义,请告诉我为什么
- 我将$userlist从SiteController传递到index.php,然后传递到$this->widget(),但在_view.php中,变量更改为$data,我缺少什么?我已经阅读了Yii网站上的文档,但我无法理解
您在ClistView的语法中犯了错误,该语法代替了您插入的用户列表中的数据提供程序,因此您将得到错误undefined userlist
这是你的语法
$this->widget('zii.widgets.CListView', array(
'userlist'=>$userlist,
'itemView'=>'_view',
));
正确的语法如所示
$this->widget('zii.widgets.CListView', array(
'dataprovider'=>$userlist,
'itemView'=>'_view',
));
$userlist
必须是一个有效的数据提供程序才能正确定义它,请遵循Daniel Vaquero 提供的步骤
在SiteController中:actionIndex()函数,"$sql"是未定义的。。。
编辑:
对于CListView Widget,您需要将"$userlist"转换为"CActiveDataProvider",如下所示:
$userlist = User::model()->findAllBySql($sql);
$userListDP = new CActiveDataProvider($userlist);
$this->render('index',array('userListDP'=>$userListDP));
在视图文件中:CListView小部件需要一个dataProvider。
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$userListDP, // CListView collect the data from 'dataProvider' param name.
'itemView'=>'_view',
));