Php 在Yii中更新查询
我在Php 在Yii中更新查询,php,mysql,yii,sql-update,Php,Mysql,Yii,Sql Update,我在Yii中有一个要求,其中我必须根据某些条件更新一个表。我必须用new\u val=previous\u value+new\u val更新列。但是代码并没有像预期的那样工作 我试过的代码是 $update = Yii::app()->db->createCommand() ->update('tbl_post', array('star'=>('star' + 1),'total'=>('total' + $ratingAjax)), 'id=:id',arra
Yii
中有一个要求,其中我必须根据某些条件更新一个表。我必须用new\u val=previous\u value+new\u val
更新列。但是代码并没有像预期的那样工作
我试过的代码是
$update = Yii::app()->db->createCommand()
->update('tbl_post', array('star'=>('star' + 1),'total'=>('total' + $ratingAjax)),
'id=:id',array(':id'=>$post_id));
在普通查询中,查询将是
UPDATE tbl_post set star= star + 1,total = total + '$ratingAjax' where id = 1
有人知道哪里出了错吗?在使用字符串时,请尝试以下方法:
$update = Yii::app()->db->createCommand()
->update('tbl_post', array('star'=>'star + 1','total'=> 'total + '.$ratingAjax),
'id=:id',array(':id'=>$post_id));
请尝试以下操作:
$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
将允许您发送一个表达式来更新列值
见:
和:uu construct-detail这应该可以完成以下工作:
Post::model()->updateCounters(
array('star'=>1),
array('total'=>$ratingAjax),
array('condition' => "id = :id"),
array(':id' => $post_id),
);
它将递增,而不是设置值
star和total
是整数字段而不是字符串。您正在构建的SQL查询是字符串而不是整数,这将生成字符串UPDATE tbl\u post set star=star+1,total=total+[$ratingAjax variable value],其中id=1
'star'+1这不是串联,这是字符串+整数,它将返回1,因此您的sql将是。。。设置星形=1…