A strange problem I have - I don't know how to describe it here

[prev] [thread] [next] [Date index for 2004/12/13]

From: karjala_lists
Subject: A strange problem I have - I don't know how to describe it here
Date: 12:07 on 13 Dec 2004
I'm trying to write a snippet of code that does money transfer from 
$user1 to $user2 (which are instances of the User CDBI class).

So I'm writing:

$user1 = User->retrieve(a);
$user2 = User->retrieve(b);
...other code...
$user1->balance($user1->balance - $amount);
$user2->balance($user2->balance + $amount);

However when the two users are the same user, and the desired result 
would be for the user's balance to remain unchanged, what happens is 
that the balance decreases by $amount.
The reason apparently is that the balances are fetched only once, during 
'retrieve', so the "$user2->balance" method inside the parentheses 
returns the initial balance, which in the last line of code will be 
reduced by $amount. (initial balance minus $amount)

Is there a way to circumvent this problem I'm facing and end up with 
constant balance?

If not, I'll resort to writing:

$dbh->do("update tblUser set balance=balance - $amount where user=$user2")

...but that's not very CDBIish (although it seems a much sounder solution).

Thank you,
 - Alexander Karelas

(message missing)

A strange problem I have - I don't know how to describe it here
karjala_lists 12:07 on 13 Dec 2004

Generated at 09:04 on 20 Dec 2004 by mariachi v0.52