更新表单

时间:2015-06-01 07:19:11

标签: drop-down-menu yii2

我想解释一下我的问题

我有一个名为companytype的下拉列表,它包含值1 + 1,1 + 2,1 + 3,1 + 4,1 + 5,1 + 6,1 + 7

在创建表单时我有一个选择值,例如:1 + 4并存储,但更新值时更改为更改为选择公司类型[提示]

<?= $form->field($model, 'companytype')->dropDownList([ '1' => '1+1', '2' => '1+2', '3' => '1+3', '4' => '1+4', '5' => '1+5', '6' => '1+6', '7' => '1+7', ], ['prompt' => 'Select Company Type', ]) ?>

这里我添加了两个你可以轻松理解我的问题的图片

创建表单的gridview gridview of created form

更新同一表格 updating the same form

更新

mycontroller代码:

public function actionCreate()
    {

        if(Yii::$app->user->can( 'create-company' ) ) 
        {
          $model = new Company();

        if ($model->load(Yii::$app->request->post()) ) {

            $model->createdat = date('Y-m-d');
            $ro = $model->relationoption;
                if($ro == 'fixed')
                {
                  $commaList = implode(', ', $model->relation);
                  $model->relation = $commaList;
                }

                $model->save();
                return $this->redirect(['view', 'id' => $model->id]); 
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }  
        }
        else
        {
            throw new ForbiddenHttpException;

        }

    }

更新的控制器代码

public function actionUpdate($id)
    {
        if(Yii::$app->user->can('update-company'))
        {
            $model = $this->findModel($id);

            if ($model->load(Yii::$app->request->post()) ) 
            {
                $model->updatedat = date('Y-m-d h:m:s');

                  $ro = $model->relationoption;
                  if($ro == 'fixed')
                  {
                    $commaList = implode(', ', $model->relation);
                    $model->relation = $commaList;
                  }
                  $model->save();
                  return $this->redirect(['view', 'id' => $model->id]);
            }
                return $this->render('update', [
                    'model' => $model,
                ]);
            }
        else
        {
            throw new ForbiddenHttpException;
        }

    }
  

有没有人回答,请回答我

1 个答案:

答案 0 :(得分:0)

我在更新操作中看不到$ model-&gt; companytype = 4。您可以将其添加到更新操作并检查。因此,您的更新操作应如下所示:

public function actionUpdate($id)
    {
        if(Yii::$app->user->can('update-company'))
        {
            $model = $this->findModel($id);
            $model->companytype = 4;

            if ($model->load(Yii::$app->request->post()) ) 
            {
                $model->updatedat = date('Y-m-d h:m:s');

                  $ro = $model->relationoption;
                  if($ro == 'fixed')
                  {
                    $commaList = implode(', ', $model->relation);
                    $model->relation = $commaList;
                  }
                  $model->save();
                  return $this->redirect(['view', 'id' => $model->id]);
            }
                return $this->render('update', [
                    'model' => $model,
                ]);
            }
        else
        {
            throw new ForbiddenHttpException;
        }

    }
相关问题