Methods of executing transactions in the thinkphp framework

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

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.