如何在新视图文件中访问模型的数据?

时间:2021-11-13 07:13:17

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:

我想在其中一个视图中访问模型的所有数据,我创建并称之为searching.php。我在业务控制器中写了一个动作,如下所示:

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.

在业务/搜索的视图中,我想访问模型(业务)的所有数据。这包括business_name,business_description等。但是当我运行此代码时,我得到错误500,未定义变量:model。

Here is my searching.php file code:

这是我的search.php文件代码:

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?

我是个傻瓜,我对yii知之甚少。如何访问视图中的所有数据?

1 个解决方案

#1


1  

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

调用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;
}

#1


1  

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

调用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;
}