Thinkphp 3.2.2 Ways to implement transaction operations

Source: Internet
Author: User
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



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.