Jump to content

Subtract 1 gives minus number


DanRz

Recommended Posts

Hey!

I have the following code in a class..

public function deductCredit(){
		$currentAmount = $this->getCredits();
		$newAmount = $currentAmount - '1';
		$this->dashboard->updateSettings("email_credit", array("count"=>$newAmount));
		return $newAmount;
  }

This gets the current credits... deducts 1 and pushes the update back...

If I run the function outside of the class it works as expected... however, if I run it within a functions a bit further down the class it always returns -1...

Any ideas would be appreciated!

Dan

Link to comment
Share on other sites

aside from something preventing this from working, there's two problems with trying to maintain a value this way -

  1. it's not concurrent safe/atomic. if more than one instance of your script is requested at the same time, they will all get the same starting value, modify it, and update it to the same ending value, resulting in only one instance of the value being modified. if you did have a need to do this, you would do it all in one single UPDATE query, which will work properly regardless of how many concurrent instances of the script is running, since the database will get the existing value, modify it, and save it while doing the necessary table locking so that this occurs during one 'atomic' operation.
  2. you should insert a row for every transaction that affects a value, so that you will have an audit trail that would let you known if a programming mistake, an accidental key press, or nefarious activity has modified the value. you would simply SUM() the +/- amounts from the relevant rows for any particular account number to get the current amount, just like your bank, credit card, utility account, ... does.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.