1

我在使用 CakePHP 更新时遇到了这个麻烦。我尝试使用该功能更新我的数据库中的一行(见下文):

function updateRow($amount, $user_id){

    $user_balance = $this->getUserBalance($user_id);

    //now update
    $balance['Balance']['id'] = $user_balance['Balance'][id];
    $balance['Balance']['amount'] = $user_balance['Balance'][amount] + $amount;

    $this->Balance->save($balance);
}

如果我称它为 ONE time ,例如:

$this->updateRow(100,5);

没关系。但是,如果我尝试调用它两次或更多次,例如:

$this->updateRow(100,5); 
$this->updateRow(200,5);
$this->updateRow(150,5);

该函数被调用,但实际上只有最后一次更新是在 db 中完成的!(例如,初始金额值为 100。在三个通话结束时,我预期的金额值为 550 但它是 250!)

你有解决方案吗?提前谢谢

4

1 回答 1

0

请参阅CakePHP 的答案:用 save() 加/减?以供参考。

您应该在这里使用原子方法 updateAll()。

于 2013-02-25T12:49:49.593 回答