代码之家  ›  专栏  ›  技术社区  ›  yii2__

控制器中的Yii2 post请求

  •  1
  • yii2__  · 技术社区  · 6 年前

       public function actionUpdate($id)
    {   
            $model = $this->findModel($id);
    
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                if(isset($_POST['submit2']) )
                {
                     $request = Yii::$app->request;
                     $test= $request->post('test', '1');
                }
                return $this->redirect(['view', 'id' => $model->ID]);
            }
    
            return $this->render('update', [
                'model' => $model,
            ]);
    
    }
    

    但是,当我单击按钮“submit2”时,“test”列仍然为空。 $request = Yii::$app->request; $test= $request->post('test', '1'); 它应该在“测试”列中写入值。

    1 回复  |  直到 6 年前
        1
  •  2
  •   ScaisEdge    6 年前

    如果你想更新列 abgerechnet 在您的模型中基于 $_POST['submit2'] 然后您应该在调用之前设置该值 model->save()

    public function actionUpdate($id)
    {   
          $model = $this->findModel($id);
    
          if ($model->load(Yii::$app->request->post()) ) {
              if(isset($_POST['submit2']) )
              {
                  $model->abgerechnet = 1;
              }
              $model->save();
              return $this->redirect(['view', 'id' => $model->ID]);
          }
    
          return $this->render('update', [
              'model' => $model,
          ]);
    
    }