<tfoot id='sBRP1'></tfoot>
      1. <small id='sBRP1'></small><noframes id='sBRP1'>

          <bdo id='sBRP1'></bdo><ul id='sBRP1'></ul>
      2. <legend id='sBRP1'><style id='sBRP1'><dir id='sBRP1'><q id='sBRP1'></q></dir></style></legend>
      3. <i id='sBRP1'><tr id='sBRP1'><dt id='sBRP1'><q id='sBRP1'><span id='sBRP1'><b id='sBRP1'><form id='sBRP1'><ins id='sBRP1'></ins><ul id='sBRP1'></ul><sub id='sBRP1'></sub></form><legend id='sBRP1'></legend><bdo id='sBRP1'><pre id='sBRP1'><center id='sBRP1'></center></pre></bdo></b><th id='sBRP1'></th></span></q></dt></tr></i><div id='sBRP1'><tfoot id='sBRP1'></tfoot><dl id='sBRP1'><fieldset id='sBRP1'></fieldset></dl></div>

        Yii2:无法按 + 1 更新列值

        时间:2023-09-24

          <tbody id='lZV3d'></tbody>
      4. <legend id='lZV3d'><style id='lZV3d'><dir id='lZV3d'><q id='lZV3d'></q></dir></style></legend>

          <small id='lZV3d'></small><noframes id='lZV3d'>

              1. <tfoot id='lZV3d'></tfoot>
                <i id='lZV3d'><tr id='lZV3d'><dt id='lZV3d'><q id='lZV3d'><span id='lZV3d'><b id='lZV3d'><form id='lZV3d'><ins id='lZV3d'></ins><ul id='lZV3d'></ul><sub id='lZV3d'></sub></form><legend id='lZV3d'></legend><bdo id='lZV3d'><pre id='lZV3d'><center id='lZV3d'></center></pre></bdo></b><th id='lZV3d'></th></span></q></dt></tr></i><div id='lZV3d'><tfoot id='lZV3d'></tfoot><dl id='lZV3d'><fieldset id='lZV3d'></fieldset></dl></div>
                • <bdo id='lZV3d'></bdo><ul id='lZV3d'></ul>

                • 本文介绍了Yii2:无法按 + 1 更新列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要在创建新记录时将列值更新为 + 1 :

                  公共函数actionCreate(){$model = new CreateBookings();if ($model->load(Yii::$app->request->post())) {Yii::$app->db->createCommand("UPDATE room_types SET total_booked = total_booked + 1 WHERE room_type = '$model->room_type'")->execute();$model->save();返回 $this->redirect(['view', 'id' => $model->id]);} 别的 {返回 $this->render('create', ['模型' =>$模型,]);}}

                  我做错了什么请指导:)

                  解决方案

                  试试这个:

                   Yii::$app->db->createCommand("UPDATE room_types SET total_booked=total_booked+1 WHERE room_type = '$model->room_type'")->execute();

                  公共函数actionCreate(){$model = new CreateBookings();if ($model->load(Yii::$app->request->post())) {$RoomType = new room_types();//房间类型替换为模型名称$RoomType->updateCounters(['total_booked' => 1]);$model->save();返回 $this->redirect(['view', 'id' => $model->id]);} 别的 {返回 $this->render('create', ['模型' =>$模型,]);}}

                  官方链接

                  I need to update column value by + 1 when new records get created :

                  public function actionCreate()
                      {
                          $model = new CreateBookings();
                          if ($model->load(Yii::$app->request->post())) {
                              Yii::$app->db->createCommand("UPDATE room_types SET total_booked = total_booked + 1 WHERE room_type = '$model->room_type' ")->execute();
                              $model->save();
                              return $this->redirect(['view', 'id' => $model->id]);
                              } else {
                                   return $this->render('create', [
                                   'model' => $model,
                                   ]);
                              }
                      }
                  

                  What am I doing wrong please guide :)

                  解决方案

                  Try this:

                   Yii::$app->db->createCommand("UPDATE room_types SET total_booked=total_booked+1 WHERE room_type = '$model->room_type' ")->execute();
                  

                  OR

                  public function actionCreate()
                      {
                          $model = new CreateBookings();
                          if ($model->load(Yii::$app->request->post())) {
                  
                      $RoomType = new room_types(); // room type replace with model name
                      $RoomType->updateCounters(['total_booked' => 1]);
                  
                        $model->save();
                              return $this->redirect(['view', 'id' => $model->id]);
                          } else {
                              return $this->render('create', [
                                  'model' => $model,
                              ]);
                          }
                      }
                  

                  Official link

                  这篇关于Yii2:无法按 + 1 更新列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:CDbConnection 打开数据库连接失败:在 Yii 中找不到驱动程序 下一篇:Yii CUserIdentity 与用户模型

                  相关文章

                • <tfoot id='t73hP'></tfoot>

                  <small id='t73hP'></small><noframes id='t73hP'>

                  • <bdo id='t73hP'></bdo><ul id='t73hP'></ul>

                  1. <legend id='t73hP'><style id='t73hP'><dir id='t73hP'><q id='t73hP'></q></dir></style></legend>
                    <i id='t73hP'><tr id='t73hP'><dt id='t73hP'><q id='t73hP'><span id='t73hP'><b id='t73hP'><form id='t73hP'><ins id='t73hP'></ins><ul id='t73hP'></ul><sub id='t73hP'></sub></form><legend id='t73hP'></legend><bdo id='t73hP'><pre id='t73hP'><center id='t73hP'></center></pre></bdo></b><th id='t73hP'></th></span></q></dt></tr></i><div id='t73hP'><tfoot id='t73hP'></tfoot><dl id='t73hP'><fieldset id='t73hP'></fieldset></dl></div>