如何转换为在视图上使用 Yii CDataProvider? [英] How to convert to use the Yii CDataProvider on view?

查看:16
本文介绍了如何转换为在视图上使用 Yii CDataProvider?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习 Yii,并查看了 Yii 文档,但仍然没有真正理解它.我仍然不知道如何在控制器和视图上使用 CDataProvider 来显示视图上可用的所有博客文章.任何人都可以根据以下内容提供建议或举例:

我的 PostController 中的 actionIndex:

公共函数 actionIndex(){$posts = Post::model()->findAll();$this->render('index', array('posts' => $posts));));

视图,Index.php:

<?php foreach ($post as $post): ?><h2><?php echo $post['title'];?></h2><?php echo CHtml::decode($post['content']);?><?php endforeach;?>

除了执行上述操作,有人可以建议如何使用 CDataProvider 来代替生成吗?

非常感谢.

解决方案

我能建议的最好方法是使用 CListViewCActiveDataProvider 在您的控制器中.所以你的代码变得有点像这样:
控制器:

公共函数 actionIndex(){$dataProvider = new CActiveDataProvider('Post');$this->render('index', array('dataProvider' => $dataProvider));}

index.php:

widget('zii.widgets.CListView', array('dataProvider'=>$dataProvider,'itemView'=>'_post',//引用名为'_post'的局部视图//'enablePagination'=>true));?>

_post.php:该文件将显示每篇文章,并作为小部件 CListView( 'itemView'=>'_post') 在 index.php 视图中.

 

<?php//echo CHtml::encode($data->getAttributeLabel('title'));echo CHtml::encode($data->title);?>

<br/><hr/><div class="post_content"><?php//echo CHtml::encode($data->getAttributeLabel('content'));echo CHtml::encode($data->content);?>

说明

基本上在控制器的索引操作中,我们正在创建一个新的 CActiveDataProvider,它提供 Post 模型的数据供我们使用,并将此数据提供者传递给索引视图.
在索引视图中,我们使用 Zii 小部件 CListView,它使用我们作为数据传递的 dataProvider 来生成列表.每个数据项都将按照我们作为属性传递给小部件的 itemView 文件中的编码呈现.此 itemView 文件将可以访问 $data 变量中的 Post 模型对象.

建议阅读:使用 Yii 1.1 和 PHP 5 进行敏捷 Web 应用程序开发
非常适合 Yii 初学者的一本书,已在 Yii 主页列出.

在没有 CListView 的情况下询问

index.php

getData();foreach ($dataArray 作为 $data){echo CHtml::encode($data->title);echo CHtml::encode($data->content);}?>

I am trying to learn Yii, and have looked at Yii documentation, but still do not really get it. I still have no idea how to use the CDataProvider on the Controller and View to display all the blog posts available on the view. Can anyone please advise or give an example based on the following:

The actionIndex in my PostController:

public function actionIndex()
{
    $posts = Post::model()->findAll();

    $this->render('index', array('posts' => $posts));
));

The View, Index.php:

<div>
<?php foreach ($post as $post): ?>
<h2><?php echo $post['title']; ?></h2>
<?php echo CHtml::decode($post['content']); ?>
<?php endforeach; ?>
</div>

Instead of doing the above, can anyone please advise how to use the CDataProvider to generate instead?

Many thanks.

解决方案

The best that i can suggest is using a CListView in your view, and a CActiveDataProvider in your controller. So your code becomes somewhat like this :
Controller:

public function actionIndex()
{
    $dataProvider = new CActiveDataProvider('Post');

    $this->render('index', array('dataProvider' => $dataProvider));
}

index.php:

<?php
  $this->widget('zii.widgets.CListView', array(
  'dataProvider'=>$dataProvider,
  'itemView'=>'_post',   // refers to the partial view named '_post'
  // 'enablePagination'=>true   
   )
  );
?>

_post.php: this file will display each post, and is passed as an attribute of the widget CListView(namely 'itemView'=>'_post') in your index.php view.

 <div class="post_title">
 <?php 
 // echo CHtml::encode($data->getAttributeLabel('title'));
 echo CHtml::encode($data->title);
 ?>
 </div>

 <br/><hr/>

 <div class="post_content">
 <?php 
 // echo CHtml::encode($data->getAttributeLabel('content'));
 echo CHtml::encode($data->content);
 ?>
 </div>

Explanation

Basically in the index action of the controller we are creating a new CActiveDataProvider, that provides data of the Post model for our use, and we pass this dataprovider to the index view.
In the index view we use a Zii widget CListView, which uses the dataProvider we passed as data to generate a list. Each data item will be rendered as coded in the itemView file we pass as an attribute to the widget. This itemView file will have access to an object of the Post model, in the $data variable.

Suggested Reading: Agile Web Application Development with Yii 1.1 and PHP 5
A very good book for Yii beginners, is listed in the Yii homepage.

Edit:

As asked without CListView

index.php

<?php
 $dataArray = $dataProvider->getData();
foreach ($dataArray as $data){
echo CHtml::encode($data->title);
echo CHtml::encode($data->content);
}
?>

这篇关于如何转换为在视图上使用 Yii CDataProvider?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆