我一直在与“看不见的”错误作斗争。 我试图做我所看到的一个简单的字段更新,但该字段永远不会更新。奇怪的是,除了以下日志外,Yii2不会输出任何错误:
info yii \ db \ ActiveRecord :: update由于验证错误,模型未更新。
控制器中的代码:
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,
]);
}
}
模型中的功能:
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;
}
情景:
"updateGuest" => ["bno_coins", "bots"],
bno_coins
和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;
不包含任何数据。似乎整个$model
不包含任何值(这可以解释验证失败的原因)。但是,根据Yii Debugger,请求包含所有必要的信息。