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

http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder

There is something here

http://www.yiiframework.com/doc/api/1.1/CDbCommandBuilder#createUpdateCounterCommand-detail

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

http://yiiframework.ru/forum/viewtopic.php?f=6&t=2022

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

$command=$connection->createCommand($sql);

$rowCount=$command->execute();



This is not query builder this is DAO B)

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

err

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( 

    'wall_message', 

    array( 

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

        ), 

    new CDbCriteria(array(

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

        "params" => array(

            "id"=>5

        )

    ))

);




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