I'm using Phalcon and I have a database in MySql. I have three tables in the database:
- user: id, name, sold
- company: id, name, cost
- transactions: id_company, id_user, cost
An user has to do a transaction if has enough money (sold). So I have to do it:
Step 1:
retrieve the sold of the user:
select sold
from user
where id='Charlie'
Step 2:
retrive the cost from the company:
select cost
from company
where id='Tango'
Step 3:
to check if the user has enough money:
if (sold-cost >= 0)
create the transaction
else
do not create the transaction.
My question is: Is there a way in order to block the db in order to do the three steps without the db could change?
I would like do this:
lock db
step 1
step 2
step 3
unlock db
But I have not found a solution for it.
I'm not sure how it implemented in Phalcon framework, but PDO extension implements transactions that can be helpful here:
PHP PDO fiddle here