gpt4 book ai didi

php - 如何访问新 View 文件中的模型数据?

转载 作者:搜寻专家 更新时间:2023-10-30 20:37:30 24 4
gpt4 key购买 nike

我想在模型的其中一个 View 中访问模型的所有数据,我创建并调用了 searching.php。我在 business Controller 中写了一个 Action ,它是这样的:

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

business/searching的 View 中,我想访问一个模型(Business)的所有数据。这包括 business_name、business_description 等。但是当我运行这段代码时,我得到了 error 500, Undefined variable: model

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

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

本人是yii bie,对yii了解甚少。如何访问 View 中的所有数据?

最佳答案

调用 render() 时,您需要在 view 中传递模型。

在你的 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;
}

关于php - 如何访问新 View 文件中的模型数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33680956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com