如何在新视图文件中访问模型的数据? [英] How can I access a model's data inside a new view file?
问题描述
我想要访问其中一个视图中的模型的所有数据,这是我做的,并调用 searching.php
。我在 business
控制器中写了一个动作,如下所示:
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');
}
在商家/搜索
我想访问模型(业务)的所有数据。这包括business_name,business_description等。但是当我运行这个代码,我得到错误500,未定义的变量:模型
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
.
这是我的searching.php文件代码:
Here is my searching.php file code:
foreach ($model as $ma)
{
echo $ma->business_name;
}
我是一个yii bie,我对yii知之甚少。如何访问视图中的所有数据?
I am a yii bie, I know very little about yii. How can I access all the data in a view?
推荐答案
您需要在在您的控制器中调用
render()
。
>
In your controller:
public function actionSearching() {
$models = Business::model()->findAll();
$this->layout='//layouts/main';
$this->render('searching', array(
'models'=>$models,
));
}
在您的视图中:
foreach($models as $model) {
echo $model->business_name;
}
这篇关于如何在新视图文件中访问模型的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!