CakePHP:add / substract with save()? [英] CakePHP: add/substract with save()?
问题描述
我试图通过Cake的save()函数执行以下操作。
I'm trying to simply perform the following via Cake's save() function.
UPDATE user SET value = value-1
但是,它似乎只能设置。它不会理解任何我传递给它增加或减少,没有人在互联网上似乎有这个问题。 :P即使通过一个基于CakePHP 2.0的软件,我发现$ this-> query()用于按增量更新!
However, it seems it can only set. It will not understand anything I pass to it to increment or subtract, and no one on the internet seems to be having this issue. :P Even when going through a full piece of software someone built on CakePHP 2.0, I'm finding $this->query() used for updating by increments! Is this really how I'll update if I don't already have the value to be setting?
(代码如下)
$data = array('id' => uid, 'value' => "Users.value = Users.value - 1");
$this->User->save($data);
推荐答案
CakePHP数据库中产生递增或递减的代码如下:
The code for producing an increment or decrement in CakePHP database is as follows:
$this->User->updateAll(array('value' => 'value - 1'), array('id' => uid));
Arun的回答不正确;你必须把 - 1在引号内,使Cake识别它是查询的一部分。否则它将尝试将所有User.value设置为-1。 请注意,您必须在第二个条件中输入要更新的列的信息(标识符)。
Arun's answer was not correct; you must put the - 1 within quotes to get Cake to recognize it is part of the query. Else it will try to set all User.value to -1. Note that you must put the information (identifiers) of the columns that you want to update on in the second condition.
这篇关于CakePHP:add / substract with save()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!