Update query in Yii(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 中的更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Yii 中的更新查询
基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01