This article mainly introduces the method of thinkphp 3.2.2 to implement transaction operation, analyzes the operation methods of transaction initiation, commit and rollback in thinkphp, and gives the complete transaction commit and rollback operation instance, the friend can refer to the following
The examples in this paper describe the way thinkphp 3.2.2 implements transactional operations. Share to everyone for your reference, as follows:
The handbook says very clearly:
5.3.19 Transaction Support
thinkphp provides transaction support for a single database , and if you want to use transactions in your application logic, you can refer to the following methods:
To start a transaction:
$User->starttrans ()
Commit TRANSACTION:
$User->commit ()
Transaction rollback:
$User->rollback ()
The transaction is for the database itself, so it can be manipulated across models.
For example:
Start Transaction $user->starttrans ()//For related business logic operations in the User model $info = M ("Info"); Instantiate Info Object $info->save ($User); Save user information if (operation succeeded) { //COMMIT 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 '] = ' testss '; M ()->starttrans (); $result = M (' feehistory ')->add ($data); $result 1 = $result 2 = true; if (!empty ($result)) { $regdelData [' level '] = ' 111 '; $result 1 = M (' Regdel ')->add ($regdelData); $regData [' level '] = ' 101 '; $result 2 = M (' reg ')->where ("Registrycode= ' 13693536752-sjb-huax-12345678 '")->save ($regData); } if (!empty ($result) &&!empty ($result 1) &&!empty ($result 2)) { M ()->commit (); $this->success (' Things submitted ',); Echo ' Things submitted '; } else{ M ()->rollback (); $this->error (' Things roll back ',); echo ' Things roll Back ';}} }
Related recommendations:
Brief discussion on simple realization of THINKPHP5 instance