问题
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