edit: phalcon 1.3.2 ubuntu 12.04.4 php 5.5.16
I have some code like this:
$matchingCards = $this->getMatches($newCard);
foreach ($matchingCards as $card) {
if ($newCard->damage > $card->damage) {
$newCard->damage -= $card->damage;
$newCard->save();
$card->damage = 0;
$card->save();
}
elseif ($newCard->damage < $card->damage) {
$card->damage -= $newCard->damage;
$card->save();
$newCard->damage = 0;
$newCard->save();
}
echo $card->damage;
}
$newCard has a damage value like 250 and is inserted into the database directly before the above code
The the first 3 card-id's is what $this->getMatches($newCard)
would return from the db.
card-id | damage |
---|---|
1 | 100 |
2 | 100 |
3 | 100 |
4 | 250 |
(card-id 4 is the $newCard)
after the foreach is run, the db looks like this:
card-id | damage |
---|---|
1 | 0 |
2 | 100 |
3 | 100 |
4 | 0 |
(card-id 4 is the $newCard)
The expected value of the cards after the foreach is:
card-id | damage |
---|---|
1 | 0 |
2 | 0 |
3 | 50 |
4 | 0 |
($newCard->damage = 250 - 100 - 100 - 50)
by replacing the $newCard->save() calls from inside the foreach with a single $newCard->save() call outside the foreach I get the proper values as like in the table above.
Its great that I've solved my own problem but I don't know why or how it happened! Am I missing something obvious here?