dongyin0628 2013-07-08 08:34
浏览 27

如何在Yii Controller和View中使用模型对象

I have following method:

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 

view file (index.php)

echo $companyData->name . "
";
echo $this->companyModel->phone . "
";
echo $this->companyModel->fax . "
";
echo $this->companyModel->cell . "
";

Any help would be appreciated.

  • 写回答

3条回答 默认 最新

  • dongyan3018 2013-07-08 09:07
    关注

    You are trying to get all the entries from the database as findAll() returns all data in multidimensional array of objects.If you need all the entries you could iterate over it in the view file and get the results as shown

    In the View File do as shown

    <?php foreach($companyData as $value){
      echo $vlaue->name . "
    ";
      echo $value->phone . "
    ";
      echo $value->fax . "
    ";
      echo $value->cell . "
    ";
     ?>
    

    With this you get all the entries from the table

    If you want to get a particular record use condition using Cdbcriteria and pass the object and get the single result

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数