如何使用模型对象在Yii的控制器和视图 [英] How to use model object in Yii Controller and View
问题描述
我有下面的方法:
public function actionIndex() {
$companyModel = Company::model()->findAll();
$supplierProductModel = SupplierProduct::model()->findAll();
$this->render('index', array(
'companyData' => $companyModel,
'supplierProductData' => $supplierProductModel,
));
}
在这里,我已经通过模型对象来渲染功能,并希望访问鉴于这些对象(主动关系型),但是当我访问了在查看其示值误差:
Here I have passed model objects to render function and want to access these objects in view (Active Relational Type) but when I am accessing its in view its showing error:
Trying to get property of non-object
查看文件(的index.php)
view file (index.php)
echo $companyData->name . "\n";
echo $this->companyModel->phone . "\n";
echo $this->companyModel->fax . "\n";
echo $this->companyModel->cell . "\n";
任何帮助将是AP preciated。
Any help would be appreciated.
推荐答案
这是发生becoz的的findAll()
It is happening becoz of findAll()
的findAll()重新运行的所有行的公司的表中多维数组,所以在这里 $ companyData STRONG>是多维数组,现在改变你的code在指数像波纹管,
findAll() reruns all rows of company table in multidimensional array, so here $companyData is multidimensional Array, now change your code in index like bellow,
<?php
foreach ($companyData as $compSub)
{
echo $compSub->name . "\n";
echo $compSub->phone . "\n";
echo $compSub->fax . "\n";
echo $compSub->cell . "\n";
}
?>
如果你想的公司数据(单行),更改您的查询像这样
If you want a company data(single row), change your query like this
$companyModel = Company::model()->findByPk($id_Of_company);
//$companyModel is single dimensional array, it has all the info of a company.
发送给浏览
$this->render('index', array(
'companyData' => $companyModel,
....................
));
现在,您可以使用显示数据波纹管code
Now you can show the data using bellow code
echo $companyData->name . "\n";
echo $companyData->phone . "\n";
echo $companyData->fax . "\n";
echo $companyData->cell . "\n";
这篇关于如何使用模型对象在Yii的控制器和视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!