【问题标题】:CListView in Yii FrameworkYii 框架中的 CListView
【发布时间】:2012-04-17 21:58:39
【问题描述】:

我已经开始研究 yii 框架,我正在关注 yii 博客教程,在本教程中学习 yii 框架的基础知识,他们在帖子视图中使用了 ClistView,但我收到了一个异常


这是我在视图文件中使用的代码:

$this->breadcrumbs=array(
    'Posts'=>array('index'),
    $model->post_id,
);

$this->menu=array(
    array('label'=>'List Posts', 'url'=>array('index')),
    array('label'=>'Create Posts', 'url'=>array('create')),
    array('label'=>'Update Posts', 'url'=>array('update', 'id'=>$model->post_id)),
    array('label'=>'Delete Posts', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->post_id),'confirm'=>'Are you sure you want to delete this item?')),
    array('label'=>'Manage Posts', 'url'=>array('admin')),
);
?>

<?php if( !empty($_GET['tag']) ) : ?>
<h1>Posts Tagged with <em><?php echo CHtml::encode($_GET['tag']); ?></em></h1>
<?php endif; ?>

<?php $this->widget('zii.widgets.CListView', array(
    'dataProvider' => $model,
    'itemView' => '_view',
    'template' => "{items}\n{pager}",
));

这是我的 PostsController 包含的内容:

/**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView($id)
    {
        $post = $this->loadModel($id);
        $this->render('view',array(
            'model'=>$post,
        ));
    }

/**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id)
    {
        if( $this->_model === NULL )
        {
            if( Yii::app()->user->isGuest )
                $condition = 'post_status='.Posts::STATUS_PUBLISHED.' or post_status='.Posts::STATUS_ARCHIVED;
            else
                $condition = '';
                
            $this->_model = Posts::model()->findByPk($id, $condition);
                
            if($this->_model === NULL)
                throw new CHttpException(404, 'The requested page does not exist.');
        }
        
        return $this->_model;
    }

我不知道我哪里做错了。

【问题讨论】:

    标签: php frameworks yii yii-components


    【解决方案1】:

    CListViewdataProvider 属性应该是CActiveDataProvider(或不同的数据提供者)。试试

    'dataProvider' => new CActiveDataProvider($model),
    

    改为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多