thinkphp6:mysql数据库使用事务(php 8.1.1 / thinkphp v6.0.10LTS)
一,创建一个order类,在其中应用事务
1,创建order的model
liuhongdi@lhdpc:/data/php/admapi$ php think make:model Order Model:app\model\Order created successfully.
2,代码:
model/Order.php
<?php declare (strict_types = 1); namespace app\model; use think\Exception; use think\facade\Db; use think\Model; /** * @mixin \think\Model */ class Order extends Model { //类名与表名不一致时在这里指定数据表名 protected $table = "orderInfo"; public function addOrderAndGoods($orderRow,$goodsRows) { //启动事务 Db::startTrans(); try { $result = Db::table("orderInfo")->insert($orderRow); if(!$result){ throw new Exception("insert order失败"); } //得到orderid $orderId = Db::table('orderInfo')->getLastInsID();; //$z = 0; //$a = 100 / $z; foreach ($goodsRows as $k => $row){ $row['orderId'] = $orderId; $result = Db::table("orderGoods")->insert($row); if(!$result){ throw new Exception("insert goods失败"); } } // 提交事务 Db::commit(); //} catch (Exception $e){ } catch (\Throwable $e){ // 事务回滚 Db::rollback(); return false; } return true; } }
说明:刘宏缔的架构森林是一个专注架构的博客,
网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/29/thinkphp6-mysql-shu-ju-ku-shi-yong-shi-wu-php-8-1-1/
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,controller/Order.php
1,创建controller
liuhongdi@lhdpc:/data/php/admapi$ php think make:controller Order Controller:app\controller\Order created successfully.
2,代码
class Order extends BaseController { /* 创建订单 */ public function addOrder() { $orderRow = [ "orderStatus"=>0, "addTime"=>date("Y-m-d H:i:s"), "price"=>"10.5", "subject"=>"订单:".date("YmdHis")."_".rand(100,999), ]; $goodsRows = [ [ "goodsId"=>15, "goodsName"=>"洽洽瓜子新年1装".date("YmdHis")."_".rand(100,999), ], ]; $order = new OrderModel(); $isSucc = $order->addOrderAndGoods($orderRow,$goodsRows); if ($isSucc){ return Result::Success("成功"); } else { return Result::Error(1,"报错"); } } }
三,测试效果:
1,成功:
查看order表:
![](https://img2020.cnblogs.com/blog/1938691/202201/1938691-20220104215710310-1478894502.png)
查看goods表:
![](https://img2020.cnblogs.com/blog/1938691/202201/1938691-20220104215710192-1353717268.png)
访问:
http://127.0.0.1:8000/order/addorder
返回:
![](https://img2020.cnblogs.com/blog/1938691/202201/1938691-20220104215710052-1795896766.png)
查看增加的数据:
![](https://img2020.cnblogs.com/blog/1938691/202201/1938691-20220104215710346-1502345935.png)
![](https://img2020.cnblogs.com/blog/1938691/202201/1938691-20220104215710135-1430380811.png)
2,失败:
此处需要model/Order.php中的
//$z = 0; //$a = 100 / $z;
此两行代码取消注释,生成一个除0错:
访问:
http://127.0.0.1:8000/order/addorder
返回:
![](https://img2020.cnblogs.com/blog/1938691/202201/1938691-20220104215710113-1519569146.png)
查看数据库:
可以看到order表和goods中的数据都没有增加
注意观察,可以发现order表中的自增id会增长,
是因为在添加数据后事务中出现异常时进行了回滚
四,查看php和thinkphp的版本:
php:
liuhongdi@lhdpc:/data/php/admapi$ php --version PHP 8.1.1 (cli) (built: Dec 20 2021 16:12:16) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.1, Copyright (c) Zend Technologies with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies
thinkphp:
liuhongdi@lhdpc:/var/www/html$ cd /data/php/admapi/ liuhongdi@lhdpc:/data/php/admapi$ php think version v6.0.10LTS