Home>Article>Backend Development> ThinkPHP 3.2.2 transaction operation methods

ThinkPHP 3.2.2 transaction operation methods

墨辰丷
墨辰丷 Original
2018-05-23 09:35:05 1565browse

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 the

manual, which is very clear:

5.3.19 Transaction support

ThinkPHP provides a single database Transaction support. If you want to use transactions in application logic, you can refer to the following method:

Start transaction:

$User->startTrans()

Commit transaction:

##

$User->commit()

Transaction rollback:

$User->rollback()

##The transaction is for the database itself, so it can be operated across models.

For example:

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

##IndexController.class.php:

startTrans(); $result = M('feehistory')->add($data); $result1 = $result2 = true; if(!empty($result)){ $regdelData['level'] = '111'; $result1 = M('regdel')->add($regdelData); $regData['level'] = '101'; $result2 = M('reg')->where("registryCode='13693536752-SJB-HUAX-12345678'")->save($regData); } if(!empty($result) && !empty($result1) && !empty($result2) ){ M()->commit(); //$this->success('事物提交',__ROOT__); echo '事物提交'; }else{ M()->rollback(); //$this->error('事物回滚',__ROOT__); echo '事物回滚'; } } }

Related recommendations:

php method of using text to count visits_php tips


Basic knowledge of php study notes


php Detailed explanation of the secondary linkage menu effect implemented by mysql_php skills


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

Statement:
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