我正在尝试列出指定用户的CGridView
个帖子,但是会出错。这样做的正确方法是什么?
Controller.php这样
public function actionListPosts($id)
{
$criteria = new CDbCriteria;
$criteria->with=array('posts');
$criteria->condition='t.id=:user_id';
$criteria->params=array(':user_id'=>$id);
$dataProvider=new CActiveDataProvider('Users', array(
'criteria' => $criteria
));
$this->render('list',array(
'dataProvider'=>$dataProvider,
));
}
View.php:
<?php $this->widget('zii.widgets.grid.CGridView',array(
'id'=>'events-grid',
'dataProvider'=>$dataProvider->posts,
'columns'=>array(
'id',
'name',
),
)); ?>
错误:
Property "CActiveDataProvider.posts" is not defined.
答案 0 :(得分:2)
如果您还希望能够按此列进行搜索和排序,请尝试http://www.mrsoundless.com/php/yii/searching-and-sorting-a-column-from-a-related-table-in-a-cgridview/
答案 1 :(得分:1)
您必须使用Post
模型的数据提供者而不是User
模型
public function actionListPosts($id)
{
$criteria = new CDbCriteria;
$criteria->condition='t.user_id=:user_id';
$criteria->params=array(':user_id'=>$id);
$dataProvider=new CActiveDataProvider('Post', array(
'criteria' => $criteria
));
$this->render('list',array(
'dataProvider'=>$dataProvider,
));
}
并在视野中
<?php $this->widget('zii.widgets.grid.CGridView',array(
'id'=>'events-grid',
'dataProvider'=>$dataProvider,
'columns'=>array(
'id',
'name',
),
)); ?>
答案 2 :(得分:0)
在控制器动作中你可以写---
$dataProvider=new CActiveDataProvider('NameofPostTableAR',array(
'criteria'=>array(
'condition'=>'post_id='.$id,)));
$this->render('list',array(
'dataProvider'=>$dataProvider,
));
并且在视图中你可以写---
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'post-grid',
'dataProvider'=>$dataprovider,
'columns'=>array(
'post_id',
'post_name',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
这里我假设你的Posts表中的字段为(post_id,post_name)
我希望你的情况可以解决..