I have been fighting with an "invisible" error. I am trying to do what I see as a simple update of a field, however the field never gets updated. Strangely, Yii2 doesn't output any error except for the following log:
info yii\db\ActiveRecord::update Model not updated due to validation error.
Code in controler:
public function actionUpdate($id)
{
$model = $this->findModel($id);
$scenario = (Yii::$app->user->identity->technology == $id) ? "updateOwner" : "updateGuest";
$model->scenario = $scenario;
$function = $scenario."Technology";
if ($model->load(Yii::$app->request->post()) && $model->validate() && Technology::$function($model)) {
return $this->render(['view', 'model' => $model->id]);
} else {
return $this->render($scenario, [
'model' => $model,
]);
}
}
The function in model:
public function updateGuestTechnology($model){
$transaction = Yii::$app->db->beginTransaction();
try
{
$technology = Technology::findOne($model->id);
$user = Yii::$app->user->identity;
$technology->scenario = "updateGuest";
$technology->bno_coins += $model->bno_coins;
$technology->bots += $model->bots;
$technology->save();
//The user query works just fine.
$user->scenario = "update";
$user->bots -= $model->bots;
$user->bno_coins -= $model->bno_coins;
$user->save();
$transaction->commit();
}
catch (Exception $e)
{
$transaction->rollBack();
Yii::error("Error during technology update by owner transaction. UserID: {$buyer->id}");
return false; //Transaction failed
}
Yii::info("Technology updated by non-owner! Technology ID: {$technology->id} . UserID: {$user->id}");
return true;
}
Scenario:
"updateGuest" => ["bno_coins", "bots"],
Rules for bno_coins
and bots
:
//bno_coins rules
["bno_coins", "required", "on" => ["create", "updateOwner", "updateGuest"]],
["bno_coins", "integer", "min" => 0, "max" => Yii::$app->user->identity->bno_coins, "on" => ["create", "updateOwner", "updateGuest"]],
//bots rules
["bots", "required", "on" => ["create", "updateOwner", "updateGuest"]],
["bots", "integer", "min" => 0, "max" => Yii::$app->user->identity->bots, "on" => ["create", "updateOwner", "updateGuest"]],
$model->errors;
Doesn't contain any data. It seems that the whole $model
doesn't contain any values (that would explain why the validation fails). However, according to Yii Debugger, the requests contain all necessary information.