Bad Request (#400) - Missing required parameters: id in YII2

Shinoda_ picture Shinoda_ · Feb 21, 2015 · Viewed 16.5k times · Source

I want to make a CRUD operation using GII Tool, but I get the error message Missing required parameters: id, when I try to save my post.

Post controller:

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

    if ($model->load(Yii::$app->request->post())) {
        $model->post_create_time=date('Y-m-d h:m:s');
        $model->save();
        return $this->redirect(['view', 'id' => $model->id_post]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

Why do I always get this error?

Answer

Abhimanyu Saharan picture Abhimanyu Saharan · Feb 22, 2015

Try

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

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

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

Make sure that you do $model->save(false) and see whether if it works.