Alt - Sun Feb 06 16:48:31 -0500 2011

Subject: Increment / decrement field

Hi im looking for mechanism allowing to increment / decrement a column for selected rows.
The problem is visible when we get some request load on server. If i do like this

$post = Post::find($id);
$post->views = $post->view + 1;
$post->save();
Other request can select between 'find' and 'save', so its not correct.
There is a way using transaction for this, but this is overkill...

Have AR some functionality for this? If not, i wanna it in wishlist xD
Note: increment/decrement should be configurable, not just +1/-1. For example this can be a 'userrating' or 'spamrating' or some 'earnpoints'


Jacques Fuentes Sun Feb 06 16:57:55 -0500 2011

I would use the following (if you are using our nightly build):

Post::update_all(array('set' => 'views = views + 1', 'conditions' => array('id' => $id));

Alt - Sun Feb 06 17:27:26 -0500 2011

Im using stable build atm.. Is there a solution for stable?
Or better i should switch to nightly ? Btw, is it stable enough for production?

Upd if someone need this - works for stable

$upd = array($id);
Book::connection()->query('update books set view=view+1 where id=?', $upd);

(1-2/2)