多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
一,创建一个order类,在其中应用事务 1,创建order的model ~~~ liuhongdi@lhdpc:/data/php/admapi$ php think make:model Order Model:app\model\Order created successfully. ~~~ 2,代码: model/Order.php [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") ~~~ <?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://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") 说明:刘宏缔的架构森林是一个专注架构的博客,地址:[https://www.cnblogs.com/architectforest](https://www.cnblogs.com/architectforest)          对应的源码可以访问这里获取: [https://github.com/liuhongdi/](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,代码 [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") ~~~ 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,"报错"); } } } ~~~ [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") ## 三,测试效果: 1,成功: 查看order表: ![](https://img.kancloud.cn/0c/e6/0ce6329f7926adef5cd25e352012a8fd_842x291.png) 查看goods表: ![](https://img.kancloud.cn/2f/d2/2fd2df32fe63bf2a39e159cff30698b2_842x284.png) 访问: ~~~ http://127.0.0.1:8000/order/addorder ~~~ 返回: ![](https://img.kancloud.cn/99/fb/99fb6ec31eef7bdcb490c669e5a74827_422x187.png) 查看增加的数据: ![](https://img.kancloud.cn/93/ce/93ce32b68695885dfff48aa799cf5f66_843x360.png) ![](https://img.kancloud.cn/bf/1a/bf1ad9f0ed47effe3438476ddd5ee071_843x310.png) 2,失败: 此处需要model/Order.php中的 ~~~ //$z = 0; //$a = 100 / $z; ~~~ 此两行代码取消注释,生成一个除0错: 访问: ~~~ http://127.0.0.1:8000/order/addorder ~~~ 返回: ![](https://img.kancloud.cn/fa/89/fa8966507ba71fe194744d69c6d35e5f_389x185.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 ~~~