ThinkPHP 3.2.2 method for implementing transaction operations, thinkphp3.2.2
This example describes how ThinkPHP 3.2.2 implements transaction operations. We will share this with you for your reference. The details are as follows:
The manual is very clear:
5.3.19 transaction support
ThinkPHP provides transaction support for a single databaseTo use transactions in the application logic, refer to the following method:
Start the transaction:
$User->startTrans()
Commit transactions:
$User->commit()
Transaction rollback:
$User->rollback()
Transactions are specific to databases, so they can be operated across models.
For example:
// Start the transaction $ User-> startTrans () in the User model // perform related business logic operations $ Info = M ("Info "); // instantiate the Info Object $ Info-> save ($ User); // save the User information if (Operation successful) {// submit the transaction $ User-> commit ()} else {// transaction rollback $ User-> rollback ()}
IndexController. class. php:
<? Phpnamespace SMS \ Controller; use Think \ Controller; class IndexController extends Controller {public function index () {$ data ['operator'] = 'tests'; M () -> startTrans (); $ result = M ('feehistory ')-> add ($ data); $ result1 = $ result2 = true; if (! Empty ($ result) {$ regdelData ['level'] = '000000'; $ result1 = M ('regdel ')-> add ($ regdelData ); $ regData ['level'] = '000000'; $ result2 = M ('reg ')-> where ("registryCode = '2017-SJB-HUAX-12345678 '") -> save ($ regData);} if (! Empty ($ result )&&! Empty ($ result1 )&&! Empty ($ result2) {M ()-> commit (); // $ this-> success ('transaction submit' ,__ ROOT __); echo 'transaction submit';} else {M ()-> rollback (); // $ this-> error ('transaction rollback' ,__ ROOT __); echo 'transaction rollback ';}}}