On Sep 7, 2011, at 02:38, Dr. Tarique Sani wrote: > On Wed, Sep 7, 2011 at 12:24 PM, Richard Neil Roque wrote: >> >> Hi i'm a new developer using CakePHP. >> I would like to know >> How can i update a field using a function. >> Example is >> >> UPDATE Users SET AGE=AGE+2+3+4 WHERE id=1??? > > If you construct your Data Array properly with the fields you want to > save you can directly call $this->User->save() > > So you already have $user['User']['id']; > > Add to it > > $user['User']['count_login'] = $count_login +1; > $user['User']['last_login'] = date('Y-m-d H:i:s'); > > Then just save it
No, you don't want to be doing that. That's not atomic. In the case of counting how many times a user has logged in it probably doesn't matter; a single user probably won't be simultaneously logging in from multiple places. But if you're counting things like downloads or page views, then you certainly might have multiple users downloading the same file or accessing the same page simultaneously, and with the race condition in the above code, you're going to miscount. You really do want to be doing it the way the OP said: by running an atomic UPDATE statement that does the increment directly in the database, not in PHP code. I'm sure it's easy to do that in CakePHP but I don't have the code handy; perhaps someone else does. -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php