This article mainly introduces the methods of executing transactions in the thinkphp framework, and analyzes the implementation techniques of using the Starttran (), Commit () and Rollback () methods in the thinkphp framework to perform transactions and rollback operations in the case of the model. A friend you need can refer to the following
The examples in this article describe the methods of executing transactions in the thinkphp framework. Share to everyone for your reference, as follows:
Function Tran () { //The flag that defines the successful failure of the transaction $mark = true; 1. Instantiate the model $model = D (' student '); 2. Turn on transaction processing $model->starttrans (); 3. LS reduced by $sql = "Update student set money=money-2000 where uname= ' ls '"; $result = $model->execute ($sql); Determine if SQL execution succeeded, and if it fails, change $mark to False if (! $result) { $mark = false; } 4. ZS increased by $sql = "Update student set money=money+2000 where uname= ' Zs '"; $result = $user->execute ($sql); Determine if SQL execution succeeded, and if it fails, change $mark to False if (! $result) { $mark = false; } $mark = false; Temporarily force change to false to test the rollback effect //5. Commit TRANSACTION //Judge $mark value, commit as ture, false rollback if ($mark) { $user->commit () ; } else { $user->rollback ();} }
Transaction-related methods in TP: all defined in Model.class.php
startTran(): Turn on transaction processing
Commit(): Commit a transaction
Rollback(): Rolling back
When you need to perform a transaction that is in a different SQL table, you only need to perform a different model definition of two SQL