Why is Controller not saving data.

Hi,

I am sure I am just doing something wrong but can for the life of me not find it. This is my action:




    public function actionSettings()

    {

    	$model = $this->findModel(1);

    	

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

		{

   			return $this->render('settings', ['model' => $model,]);

		}

    	else

    	{

	    	return $this->render('settings', ['model' => $model,]);

    	}

    }



Yes I am hardcoding the $id value, but it all runs fine, but nothing gets updated. It is somehow working because it gets data from db and displays it in my view, but changing and submitting makes no change to the db.

Thanks

Hi,


$model = $this->findModel(1);

==> No use because you override with your data POST.

Next, in if/else you display the same then you can’t see an error on saving : you just display the same model that you think you’ve saved.

Maybe the Id isn’t in the post.

You can check the logs (in runtime) to verify, or display something different in else to verify too.

Hope that help,

Jok

this because $model->load(Yii::$app->request->post()) returns false

i don’t dig why it is so, but it really fails for me too.

so far i have used this way

$model->attributes = \Yii::$app->request->post();

if(model->save()) {

OK

}