donglang2010 2013-12-09 10:30
浏览 50
已采纳

Yii dataProvider主题main.php

In my SiteController.php

public function actionIndex()
{
    // renders the view file 'protected/views/site/index.php'
    // using the default layout 'protected/views/layouts/main.php'
    //$this->render('index');

    $dataProviderUser=new CActiveDataProvider('User',array(
                'pagination'=>array(
                    'pageSize'=>20,
                ),
            ));
    $dataProviderDomain = new CActiveDataProvider('Domain');

    $this->render('index',array(
    'dataProvider1'=>$dataProviderUser,
    'dataProvider2'=>$dataProviderDomain
    ));
}

In my themes/k/views/layout/main.php

        <?php 
        if(!Yii::app()->user->isGuest)
        {
        $this->widget('zii.widgets.CListView', array(
            'dataProvider'=>$dataProvider2,
            'itemView'=>'_view',));
        ?>

This error appears: Undefined variable: dataProvider2

If i do this in my main.php:

<?php 
        $dataProvider2 = new CActiveDataProvider('Domain');
        if(!Yii::app()->user->isGuest)
        {
        $this->widget('zii.widgets.CListView', array(
            'dataProvider'=>$dataProvider2,
            'itemView'=>'_view',));
        ?>

That works fine so far. But if I go to the User Profil:

/user/view/id/5

Property "Domain.username" is not defined.

So in my User _view.php Yii seems to take the Domain Dataprovider.

How do I past those dataProviders to my layout main.php file? The SiteController.php doesn't seem to do that right.

Would be very nice, if someone has an idea here. Thanks in advanced.

  • 写回答

1条回答 默认 最新

  • dongmai6666 2013-12-09 11:42
    关注

    Variables that you have defined in your controller action are only available to immediate view files that are being used to render a view. They are not available to layouts. However, layouts will be able to use public methods and properties of the controller. So, if you want to pass a variable to your layout you need to declare it as a property of the controller. There are two ways of doing this.

    Firstly, you can just create a public variable.

    Class YourController extends CController{
    
    public $dataProvider2;
    
    public function actionIndex(){
    // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        //$this->render('index');
    
        $dataProviderUser=new CActiveDataProvider('User',array(
                    'pagination'=>array(
                        'pageSize'=>20,
                    ),
                ));
        $dataProviderDomain = new CActiveDataProvider('Domain');
    
    //Added new line here
    $this->dataProvider2 = $dataProviderDomain;
        $this->render('index',array(
        'dataProvider1'=>$dataProviderUser,
        'dataProvider2'=>$dataProviderDomain
        ));
    }
    
    }
    

    $this->dataProvider is now available in your view file, but don't foorget to check that it exists before using it.

    The other method is to use the magic getter method from Yii.

    In your model, describe a method like this;

    public function getdataProvider2(){
    return $this->_dataProvider2;
    }
    

    and you'll need a property;

    private $_dataProvider2;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么实现输入一个要删除的数后删除后显示剩余数再输入再删除显示剩余数(语言-c语言)
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误