thinkphp6:mysql数据库使用事务(php 8.1.1 / thinkphp v6.0.10LTS)

发布时间:2022-06-26 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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:appmodelOrder created successfully.
2,代码:
model/Order.php
<?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

二,controller/Order.php

1,创建controller
liuhongdi@lhdpc:/data/php/admapi$ php think make:controller Order
Controller:appcontrollerOrder 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表:

thinkphp6:mysql数据库使用事务(php 8.1.1 / thinkphp v6.0.10LTS)

查看goods表:

thinkphp6:mysql数据库使用事务(php 8.1.1 / thinkphp v6.0.10LTS)

访问:
http://127.0.0.1:8000/order/addorder
返回:

thinkphp6:mysql数据库使用事务(php 8.1.1 / thinkphp v6.0.10LTS)

查看增加的数据:

thinkphp6:mysql数据库使用事务(php 8.1.1 / thinkphp v6.0.10LTS)

thinkphp6:mysql数据库使用事务(php 8.1.1 / thinkphp v6.0.10LTS)

2,失败:
此处需要model/Order.php中的
            //$z = 0;
            //$a = 100 / $z;
此两行代码取消注释,生成一个除0错:
访问:
http://127.0.0.1:8000/order/addorder
返回:

thinkphp6:mysql数据库使用事务(php 8.1.1 / thinkphp v6.0.10LTS)

查看数据库:
可以看到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 

 

脚本宝典总结

以上是脚本宝典为你收集整理的thinkphp6:mysql数据库使用事务(php 8.1.1 / thinkphp v6.0.10LTS)全部内容,希望文章能够帮你解决thinkphp6:mysql数据库使用事务(php 8.1.1 / thinkphp v6.0.10LTS)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:数据库