duan7772 2015-10-10 18:15
浏览 65
已采纳

在yii2中调用非对象的成员函数save()

HTML Code:

<div class="btn-group" data-toggle="buttons">
   <label class="btn btn-default">
     <span class="glyphicon glyphicon-ok"></span>
     <input type="checkbox" value="Wiring" name="type_of_service_requires" autocomplete="off">Wiring                        
   </label>

   <label class="btn btn-default electric-ppoints">
     <span class="glyphicon glyphicon-ok"></span>
     <input type="checkbox" value="Powerpoints" name="type_of_service_requires" autocomplete="off" checked> Powerpoints
   </label>

ASAP Next few days Residential Commercial

The above line is showing output from HTML code.

Controller code:

 public function actionGetQuotes()
 {
    $model = new GetQuotesForm();

    if ($model->load(Yii::$app->request-> post()))
    {

          $model= array_key_exists('type_of_service_requires', $_POST)? $_POST['type_of_service_requires']:"";

          $model= array_key_exists('residential_commercial', $_POST)? $_POST['residential_commercial']:"";

          $model= array_key_exists('work_start_when', $_POST)? $_POST['work_start_when']:"";

          if ($model->save(false))
          {
               Yii::$app->getSession()->setFlash('success', 'New Info Was Saved.');
          }
    }
    return $this->render('get-quotes',['model' => $model,]);
}

It's showing: PHP Fatal Error – yii\base\ErrorException

Call to a member function save() on a non-object

Can anyone please help about this issue?

  • 写回答

1条回答 默认 最新

  • dongwen2896 2015-10-11 17:13
    关注

    The problem is happening here for bellow lines-

          $model= array_key_exists('type_of_service_requires', $_POST)? $_POST['type_of_service_requires']:"";
    
          $model= array_key_exists('residential_commercial', $_POST)? $_POST['residential_commercial']:"";
    
          $model= array_key_exists('work_start_when', $_POST)? $_POST['work_start_when']:"";
    

    Comment all 3 lines and try to submit the form again. It should work without error.

    If you want to save submitted values with all above 3 lines then modify them in bellow format-

          $model->type_of_service_requires = $_POST['type_of_service_requires'];
    
          $model->residential_commercial = $_POST['residential_commercial'];
    
          $model->work_start_when = $_POST['work_start_when'];
    

    Now model->save(false) will work after form submission. But if the form submission contains any empty value for any of three attributes then it will display error.

    To solve this you can use following way for all three attributes-

     if(isset( $_POST['type_of_service_requires'] ))
        $model->type_of_service_requires = $_POST['type_of_service_requires'];
    
     if(isset( $_POST['residential_commercial'] ))
        $model->residential_commercial = $_POST['residential_commercial'];
    
     if(isset( $_POST['work_start_when'] ))
        $model->work_start_when = $_POST['work_start_when'];
    

    Please add comments if the solution does not work. Add your error details in comments.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里