smeto
(Matej Smeto)
August 23, 2009, 1:03pm
1
Hello,
As it doesnt work this way:
$attributes=array(
'column'=>'column-1',
);
$update = Post::model();
$update->updateAll($attributes);
could you point me to an alternative way to increase or decrease value of numeric column using ActiveRecord?
Any help appreciated
pestaa
(Pestaa)
August 23, 2009, 1:10pm
2
updateCounters() Welcome to the forums!
sebi
(Zemog86)
August 23, 2009, 1:18pm
3
/**
* Postprocessing after the record is saved
*/
protected function afterSave()
{
MyModel::model()->updateCounters(array('myValue'=>1));
}
protected function afterDelete()
{
MyModel::model()->updateCounters(array('myValue'=>-1));
}
Not tested, but it should work.
Maybe you should take a look here:
http://www.yiiframework.com/doc/blog/comment.model
greetz from Germany
Sebi
1 Like
smeto
(Matej Smeto)
August 23, 2009, 1:22pm
4
thanx, somehow i didnt noticed that method before:)
smeto
(Matej Smeto)
August 23, 2009, 1:36pm
5
and one more thing about this:
if i need some column to have a value of another column, what’s the trick to do that with AR?
for example following sql
update myTable set column=otherColumn
thanx in advance
pestaa
(Pestaa)
August 23, 2009, 1:44pm
6
update(array(‘column’=>CDbExpression(‘otherColumn’))); might work, I haven’t tested though.