2016-09-30 178 views
0

每當我試圖創建一個事件,點擊創建後說:壞請求(#400)缺少必要的參數:ID在yii2

壞請求(#400)缺少必要的參數:ID

我曾試過做$model->save(false);,但是當我這樣做時,它只上傳文本,而不是圖片。

我eventController代碼:

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

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

      // get the instance of the uploaded file 
      $imageUpload = $model ->event_name; 
      $model->file =UploadedFIle::getInstance($model,'file'); 
      if($model->file) { 
     $model->event_image='images/'.$imageUpload.'.'.$model->file->extension; 
      $model->save(); 
     $model->file->saveAs('images/'.$imageUpload.'.'.$model->file->extension); 
} else { 
    $model->save(); 
} 




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

你是什麼形式的行動之前設置或不 print_r($model->attributes);屬性?它指向創造行動嗎? –

+0

謝謝你們!我發現我的錯誤,並解決了它 –

回答

1

似乎'id' => $model->event_id正在尋找lastInsertID

試試這個

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

而且你可以嘗試讓領域模型規則safe

[['field1','field2','field3'], 'safe'], 

最後,你可以嘗試$model->load(Yii::$app->request->post())後檢查,如果該模型已經save(false)

+0

感謝它解決了 –

+0

很高興聽到,不要忘記接受答案,如果它幫助你 –

+0

不適合我 – faisal1208