ThinkPHP 3.2.2 How to implement transaction operations

不言
Release: 2023-03-25 07:04:01
Original
2594 people have browsed it

This article mainly introduces the method of implementing transaction operations in ThinkPHP 3.2.2, briefly analyzes the startup, submission, rollback and other operation methods of transactions in thinkPHP and gives complete transaction submission and rollback operation examples. Friends can refer to

This article describes the method of implementing transaction operations in ThinkPHP 3.2.2. Share it with everyone for your reference, the details are as follows:

The manual says it very clearly:

5.3.19 Transaction support

ThinkPHP provides single The database's transaction support. If you want to use transactions in application logic, you can refer to the following method:

Start a transaction:

$User->startTrans()
Copy after login

Commit transaction:

$User->commit()
Copy after login

Transaction rollback:

$User->rollback()
Copy after login

Transactions are for the database itself, so they can operate across models.

For example:

// 在User模型中启动事务
$User->startTrans()
// 进行相关的业务逻辑操作
$Info = M("Info"); // 实例化Info对象
$Info->save($User); // 保存用户信息
if (操作成功){
  // 提交事务
  $User->commit()
}else{
  // 事务回滚
  $User->rollback()
}
Copy after login

IndexController.class.php: ##

<?php
namespace SMS\Controller;
use Think\Controller;
class IndexController extends Controller {
  public function index(){
    $data[&#39;operator&#39;] = &#39;Testss&#39;;
    M()->startTrans();
    $result = M(&#39;feehistory&#39;)->add($data);
    $result1 = $result2 = true;
    if(!empty($result)){
      $regdelData[&#39;level&#39;] = &#39;111&#39;;
      $result1 = M(&#39;regdel&#39;)->add($regdelData);
      $regData[&#39;level&#39;] = &#39;101&#39;;
      $result2 = M(&#39;reg&#39;)->where("registryCode=&#39;13693536752-SJB-HUAX-12345678&#39;")->save($regData);
    }
    if(!empty($result) && !empty($result1) && !empty($result2) ){
      M()->commit();
      //$this->success(&#39;事物提交&#39;,);
      echo &#39;事物提交&#39;;
    }else{
      M()->rollback();
      //$this->error(&#39;事物回滚&#39;,);
      echo &#39;事物回滚&#39;;
    }
  }
}
Copy after login
Related recommendations:

A brief discussion on the simple implementation of thinkphp5 instance



##

The above is the detailed content of ThinkPHP 3.2.2 How to implement transaction operations. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!