incrementing and decrementing via query builder

very common task to do for example count = count - 1

But how to do it with query builder?

here, no word about it… Tried to use simple update(), dont know how to do it there, it just assign ‘count - 1’ as text

There is something here

But i Dont know how to use it…

Do you try.

SET `count` = `count` - 1

I want to use query builder

Seems to me, there is no good way to do it via query builder (qb s*cks ? >:( the most common task and you cant do it), strange

Here the issue described (in russian) but with examples

Should work like this:

//Increment thread.views by one

Yii::app()->db->createUpdateCounter('{{thread}}', array(

  'views' => 1,


$sql = 'update table set count=count-1';

$connection=Yii::app()->db;   // assuming you have configured a "db" connection



This is not query builder this is DAO B)

I’m not a noob that can’t write simple sql query :lol:


Ouchies… I’ve been a bit quick about that. Try this:

Yii::app()->db->commandBuilder->createUpdateCounterCommand('{{thread}}', array(

  'views' => 1,


See the link I gave…

$cmd = Yii::app()->db->commandBuilder->createUpdateCommand( 



        "id" => new CDbExpression( "rating + :rating" , array(":rating"=>5))


    new CDbCriteria(array(

        "condition" => "id = :id" , 

        "params" => array(





This is how looks update table set b=b+1 where id=3

Maybe some pro will help? still cant belive that this is the only solution