How can I access a model's data inside a new view file?

大城市里の小女人 提交于 2020-01-04 05:23:06

问题


I want to access all the data of a model in one of its views, which I made and called searching.php. I wrote an action in the business controller which is like this:

public function actionSearching()
    {
        // using the default layout 'protected/views/layouts/main.php'
        $this->layout='//layouts/main';
            $this->render('searching');
    }

In the view of business/searching I want to access all the data of a model (Business). This includes business_name, business_description etc. But when I run this code I get error 500, Undefined variable: model.

Here is my searching.php file code:

foreach ($model as $ma)
{
    echo $ma->business_name;
}

I am a yii bie, I know very little about yii. How can I access all the data in a view?


回答1:


You need to pass your models in the view when you call render().

In your controller:

public function actionSearching() {
    $models = Business::model()->findAll();

    $this->layout='//layouts/main';
    $this->render('searching', array(
        'models'=>$models,
    ));
}

In your view:

foreach($models as $model) {
    echo $model->business_name;
}


来源:https://stackoverflow.com/questions/33680956/how-can-i-access-a-models-data-inside-a-new-view-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!