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.

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

报告相同问题?