Let me adjust my question (was: A strange problem I have...)

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

From: karjala_lists
Subject: Let me adjust my question (was: A strange problem I have...)
Date: 12:43 on 13 Dec 2004
Let me adjust my question:

I have retrieved an object from a table/class.
Then I altered the contents of the table using: $dbh->do("update table 
set balance = balance - 1 where user = 5"), because this is a more 
reliable way to alter balances in money transactions than CDBI (I guess).

Question: How do I make object $a up to date with the database, and not 
return the 'old' balance when I do $a->balance ?

Thank you,
 - Alexander Karelas




karjala_lists@xxxxxxx.xxx wrote:

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

Let me adjust my question (was: A strange problem I have...)
karjala_lists 12:43 on 13 Dec 2004

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