dckurushin
(Diavolonok)
September 27, 2011, 5:47pm
1
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…
frocco
(Farocco)
September 27, 2011, 5:53pm
2
Do you try.
SET `count` = `count` - 1
dckurushin
(Diavolonok)
September 27, 2011, 5:54pm
3
I want to use query builder
dckurushin
(Diavolonok)
September 27, 2011, 5:55pm
4
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,
));
frocco
(Farocco)
September 27, 2011, 6:05pm
6
$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();
dckurushin
(Diavolonok)
September 27, 2011, 6:13pm
7
This is not query builder this is DAO
I’m not a noob that can’t write simple sql query
Ouchies… I’ve been a bit quick about that. Try this:
Yii::app()->db->commandBuilder->createUpdateCounterCommand('{{thread}}', array(
'views' => 1,
));
dckurushin
(Diavolonok)
September 27, 2011, 6:22pm
10
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
dckurushin
(Diavolonok)
September 28, 2011, 6:35am
11
Maybe some pro will help? still cant belive that this is the only solution