dshdb64088 2015-03-11 16:59
浏览 134

由于验证错误,模型未更新

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来