M
M
McMike2018-03-27 12:53:57
Yii
McMike, 2018-03-27 12:53:57

How to make nested transactions in Yii2?

I'll ask a few questions using fake code as an example.
There is such code:
$transaction = Yii::$app->db->beginTransaction();
$saveData1();
somefunction();
$transaction->commit();
Inside somefunction(), a transaction is also called:
$transaction = Yii::$app->db->beginTransaction();
$transaction->commit();
$a = getSavedData1();
Nested transactions are obtained in the code.
Question 1: Is it the same transaction or are they two different transactions? Will the $a variable be available if the outer transaction in which it was saved has not been committed, but the inner one has already been committed?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Timofeev, 2018-03-27
@webinar

What does it have to do with transactions, and yii? The issue is in the scope plane of variables in php , the variable will not be visible outside of the function and is not saved for the next function call.

P
Papa, 2018-03-27
Stifflera @PapaStifflera

There are no transactions in Yii. Absolutely not. They are in the database. Not all DBMS support nested transactions. See the doc for the one you are using.

A
Alexander N++, 2018-03-28
@sanchezzzhak

in what sequence the transactions were launched in the same sequence, you need to close them, these are the requirements of the PDO driver to the database (AR works through PDO, whether there are nested transactions or not - see the DBMS manual)
You need to work like this:
transaction begin 1
- transaction begin 2
- begin transaction 3
- commit/rollback
transaction 3 - commit/rollback transaction 2
commit/rollback transaction 1
ladder, otherwise they don't work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question