Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add up value using database field value with savefield in cakephp

Tags:

sql

cakephp

my question is pretty simple but hard to find an answer for though search engines.

I simply want to update a field in the database, using that fields old value to add another value. I'm using the following at the moment:

$this->Advertisement->saveField('total_views', '(total_views + 1)', false);

But this gives me the next query:

UPDATE `advertisement` SET `total_views` = '(total_views +1)', `modified` = '2011-08-26 10:44:58' WHERE `advertisement`.`id` = 16

This is wrong and it should be:

UPDATE `advertisement` SET `total_views` = (total_views +1), `modified` = '2011-08-26 10:44:58' WHERE `advertisement`.`id` = 16

The problem is where it puts (total_views +1) between quotes.

Does anyone have an idea on how to get this working?

like image 719
Kevin Vandenborne Avatar asked Feb 24 '23 04:02

Kevin Vandenborne


1 Answers

$this->Advertisement->updateAll(
array('Advertisement.total_views' => 'Advertisement.total_views + 1'),
array('Advertisement.id' => 1)
);
like image 151
chetanspeed511987 Avatar answered Apr 08 '23 09:04

chetanspeed511987