E
E
entermix2015-12-08 00:34:57
PHP
entermix, 2015-12-08 00:34:57

How to properly organize balance write-off?

Let's take this service as an example:
A user registers on a site where he can replenish his account and create a community, a guest of this community can request a certain service that the community creator pays for.
Let's implement it like this:

$this->balance -= $sum;
$this->save();

+ there is a record in another table (reason, amount, balance)
Is this correct? Let's say there are 100500 guests, N users request the service at the same time, will the funds be debited correctly? If not, how to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2015-12-08
@entermix

If your operation is NOT ATOMIC, then you need to wrap it in a transaction
Partially described here - php.net/manual/en/pdo.transactions.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question