Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Yii中更新查询_Php_Mysql_Yii_Sql Update - Fatal编程技术网

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…