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


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

HTML代码:

<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>

尽快接下来的几天住宅商业

上面一行显示的是HTML代码的输出。

控制器代码:

 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,]);
}

显示:PHP致命错误- yii'base'ErrorException

在非对象上调用成员函数save()

有谁能帮忙解决这个问题吗?

下面的行出现问题-

      $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']:"";

注释所有3行并尝试再次提交表单。它应该可以正常工作。

如果你想用以上3行保存提交的值,那么修改它们的格式如下-

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

现在model->save(false)将在表单提交后工作。但是,如果表单提交包含三个属性中的任何一个空值,则会显示错误。

要解决这个问题,您可以对所有三个属性使用以下方法-

 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'];

如果解决方案不工作,请添加评论。在注释中添加错误细节。