如何在新的视图文件中访问模型数据? [英] 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/searching
的视图中,我想访问模型(Business)的所有数据.这包括business_name,business_description等.但是,当我运行此代码时,出现错误500,未定义变量:model
.
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
.
这是我的search.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()
时,您需要在 view
中传递模型.
You need to pass your models in the view
when you call render()
.
在您的控制器中:
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屋!