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 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启