Decrease value of a column in all rows using ActiveRecord

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

updateCounters() :) Welcome to the forums!





/**

	 * 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

thanx, somehow i didnt noticed that method before:)

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

update(array(‘column’=>CDbExpression(‘otherColumn’))); might work, I haven’t tested though.