1

可能重复:
按值递增 Cakephp 数据库字段

我想将一个整数添加到我的数据库中的现有 int (不是另一列,实际上是在数学上将一个数字添加到现有值)。

说我有一个表格条目

id | Entry | total

如果我想在特定条目的总数中加 1,我可以这样做吗?

$this->Entry->find($id);
$this->Entry->saveField('total' + 1, true);

我也尝试将其添加到 EntriesController 中的函数中,但它不起作用

$this->Entry->updateAll(array('Entry.total'=>'Entry.total+1'),array('Entry.id' => $id));

这是我尝试执行此操作的另一种方法

//in the controller
$this->Entry->increaseOne($id);

//function in the model called by the controller
public function increaseOne($id){
    $this->Entry->updateAll(array('Entry.total'=>'Entry.total+1'),array('Entry.id' => $id));
}

编辑 - 我让它工作

4

0 回答 0