0

我正在尝试列出CGridView指定用户的帖子,但出现错误。这样做的正确方法是什么?

控制器.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,
    ));
}

查看.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.
4

3 回答 3

2

如果您还希望能够按此列进行搜索和排序,请尝试http://www.mrsoundless.com/php/yii/searching-and-sorting-a-column-from-a-related-table-in -a-cgridview/

于 2013-01-28T23:18:41.713 回答
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',
    ),
)); ?>
于 2013-01-28T07:27:12.897 回答
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)

我希望它适用于你的情况..

于 2013-01-25T13:45:12.197 回答