Daniel O'Leary
Daniel O'Leary

Reputation: 191

Yii2 Active Record class load() or save() not updating data

The following is the controller code that seems to be working through the if statement but the data in the db is not updating.

public function actionUpdate($id)
{
    $model = $this->findModel($id);

    if ($_model = Yii::$app->request->post() && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('update', [
            'model' => $model,
        ]);
    }
}

I suspect the problem lies in something to do with the fact that it is part of a CRUD code for of users and the form only updates a handful of fields (excluding password, authKey, etc).

Please help.

Thanks

Upvotes: 2

Views: 9151

Answers (1)

Akshay Vanjare
Akshay Vanjare

Reputation: 655

Change your code from

if ($_model = Yii::$app->request->post() && $model->save()) {

to :

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

Upvotes: 4

Related Questions