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

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

图片转代码服务由CSDN问答提供 功能建议

HTML代码:

 &lt; div class =“btn  -group“data-toggle =”buttons“&gt; 
&lt; label class =”btn btn-default“&gt; 
&lt; span class =”glyphicon glyphicon-ok“&gt;&lt; / span&gt; 
&lt;  ; input type =“checkbox”value =“Wiring”name =“type_of_service_requires”autocomplete =“off”&gt; Wiring 
&lt; / label&gt; 
 
&lt; label class =“btn btn-default electric-ppoints”  &gt; 
&lt; span class =“glyphicon glyphicon-ok”&gt;&lt; / span&gt; 
&lt; input type =“checkbox”value =“Powerpoints”name =“type_of_service_requires”autocomplete =“off”checked&gt;  Powerpoint 
&lt; / label&gt; 
   
 
 

ASAP 下n天 n 住宅 商业

上述行显示HTML代码的输出。

控制器代码:

  public function actionGetQuotes()
 {
 $ model = new GetQuotesForm(); 
 
 if if($ model-&gt; load)  (Yii :: $ app-&gt; request-&gt; 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 if($ model-&gt; save(false))
 {
 Yii :: $ app-&gt; getSession() - &gt; setFlash('success  ','新信息已保存。'); 
} 
} 
返回$ this-&gt; render('get-quotes',['model'=&gt; $ model,]); 
} \  n   
 
 

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

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

有人可以帮忙解决这个问题吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题