Incrementing a field in CakePHP like Hits & Votes

Incrementing a field in CakePHP like Hits & Votes

Let’s say you have an application, where users can place votes for their favorite products and you’d like to increment the current number of votes by one or count number of hits(Number of page views)

It’s very easy by using updateAll:

$this->Product->updateAll(array('Product.vote'=>'Product.vote+1'), array('Product.id'=>40));

You may want to restrict Product model by using unbindModel, containable, etc.

Also, you don’t have to pass in the second argument to updateAll, if you wish to update all records in your table.

Pin It on Pinterest

Share This