1

I've this trouble with CakePHP update. I try to update a row in my DB with that function (see below):

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);
}

If I call it ONE time , like:

$this->updateRow(100,5);

it's all ok. But if I try to call it two or more times , like:

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

the function was called but only the last update was actually done in db !! (eg. initial amount value was 100. At the end of three call I expected amount value was 550 but it is 250 !)

Have you solutions for this ? thx in advance

NikoDev
  • 15
  • 1
  • 9
  • Why not using atomic queries here? updateAll() in your case - see other questions and solutions which would render this a duplicate. tip "increment + updateAll" as keywords. – mark Feb 25 '13 at 11:06
  • I search for this first, but all the solutions seems not work for me... I try with updateAll method, with SQL query method , but I cannot solve my problem .. Any suggestion ? – NikoDev Feb 25 '13 at 11:16
  • What is the code you tried for updateAll()? – mark Feb 25 '13 at 11:42
  • I found the solution here :--> http://stackoverflow.com/a/11901870/2004493 Was a problem with my updateAll sintax . thx – NikoDev Feb 25 '13 at 11:51

1 Answers1

0

Please see the answers at CakePHP: add/substract with save()? for reference.

You should use the atomic method updateAll() here.

Community
  • 1
  • 1
mark
  • 21,691
  • 3
  • 49
  • 71