脚本宝典收集整理的这篇文章主要介绍了thinkphp6:mysql数据库使用事务(php 8.1.1 / thinkphp v6.0.10LTS),脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
liuhongdi@lhdpc:/data/php/admapi$ php think make:model Order Model:appmodelOrder created successfully.
<?php declare (strict_types = 1); namespace appmodel; use thinkException; use thinkfacadeDb; use thinkModel; /** * @mixin thinkModel */ 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://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
liuhongdi@lhdpc:/data/php/admapi$ php think make:controller Order Controller:appcontrollerOrder created successfully.
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,"报错"); } } }
http://127.0.0.1:8000/order/addorder
//$z = 0; //$a = 100 / $z;
http://127.0.0.1:8000/order/addorder
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
liuhongdi@lhdpc:/var/www/html$ cd /data/php/admapi/ liuhongdi@lhdpc:/data/php/admapi$ php think version v6.0.10LTS
以上是脚本宝典为你收集整理的thinkphp6:mysql数据库使用事务(php 8.1.1 / thinkphp v6.0.10LTS)全部内容,希望文章能够帮你解决thinkphp6:mysql数据库使用事务(php 8.1.1 / thinkphp v6.0.10LTS)所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。