簡體   English   中英

Yii2:錯誤的請求(#400)缺少必需的參數:id

[英]Yii2 : Bad Request (#400) Missing required parameters: id

我的控制器:

public function actionCreate()
    {
            $model = new CreateBookings();

            if ($model->load(Yii::$app->request->post())) 
            {
                $imageName = $model->primary_name;
                $model->file = UploadedFile::getInstance($model, 'file');
                $model->file->saveAs('uploads/'.$imageName.'.'.$model->file->extension);
                $model->id_image = 'uploads/'.$imageName.'.'.$model->file->extension;
                $model->save();

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

    }

在提交表單時出現此錯誤,不知道它是怎么了。.嘗試了$model->save(false); ..但效果不佳

嘗試使用getPrimaryKey()方法:

public function actionCreate()
    {
            $model = new CreateBookings();

            if ($model->load(Yii::$app->request->post())) 
            {
                $imageName = $model->primary_name;
                $model->file = UploadedFile::getInstance($model, 'file');
                $model->file->saveAs('uploads/'.$imageName.'.'.$model->file->extension);
                $model->id_image = 'uploads/'.$imageName.'.'.$model->file->extension;
                if($model->save())
                {

                   $lastInsertID = $model->getPrimaryKey();
                   return $this->redirect(['view', 'id' => $lastInsertID]);
                }
               else
               {
                   // print_r($model->getErrors()); => check whether any validation errors are there
               }


            } else 
            {
                return $this->render('create', [
                    'model' => $model,
                ]);
            }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM