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

        <bdo id='Zu9j6'></bdo><ul id='Zu9j6'></ul>
      <legend id='Zu9j6'><style id='Zu9j6'><dir id='Zu9j6'><q id='Zu9j6'></q></dir></style></legend>

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

    1. Yii 中的更新查询

      时间:2023-09-24
      1. <small id='0521A'></small><noframes id='0521A'>

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

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

                问题描述

                我在 Yii 中有一个要求,我必须根据某些条件更新一张表.我必须用 new_val = previous_value + new_val 更新列.但是代码没有按预期工作.

                I have one requirement in Yii where I have to update one table based on some condition. And I have to update the column with new_val = previous_value + new_val. But the code is not working as expected.

                我试过的代码是

                $update = Yii::app()->db->createCommand()
                ->update('tbl_post', array('star'=>('star' + 1),'total'=>('total' + $ratingAjax)),
                'id=:id',array(':id'=>$post_id));
                

                在正常查询中,查询将是

                In normal query the query will be

                UPDATE tbl_post set star= star + 1,total = total + '$ratingAjax' where id = 1
                

                有人知道错误在哪里吗?

                Anybody knows where is mistake?

                推荐答案

                尝试以下操作:

                $update = Yii::app()->db->createCommand()
                    ->update('tbl_post', 
                        array(
                            'star'=>new CDbExpression('star + 1'),
                            'total'=>new CDbExpression('total + :ratingAjax', array(':ratingAjax'=>$ratingAjax))
                        ),
                        'id=:id',
                        array(':id'=>$post_id)
                    );
                

                使用 CDbExpression 将允许您发送一个表达式来更新列值.

                Using CDbExpression will allow you to send an expression for what to update the column value to be.

                参见:http://www.yiiframework.com/doc/api/1.1/CDbCommand#更新细节

                和:http://www.yiiframework.com/doc/api/1.1/CDbExpression#__构造细节

                这篇关于Yii 中的更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:yii2:使复选框被选中 下一篇:防止在 Yii 中加载特定的 CSS 文件

                相关文章

                    <bdo id='ZeIxo'></bdo><ul id='ZeIxo'></ul>

                  <tfoot id='ZeIxo'></tfoot>

                  1. <legend id='ZeIxo'><style id='ZeIxo'><dir id='ZeIxo'><q id='ZeIxo'></q></dir></style></legend>

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

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