[已解决]webman使用事务无效,回滚了,数据依然添加成功

shyrank

bug描述

webman使用事务无效,回滚了,数据依然添加成功,使用的是 illuminate/database

程序代码或配置

<?php
declare(strict_types=1);

namespace App\api\controller\Base;
use App\common\model\collections\Collections;
use App\common\model\collections\CollectionsWarehouse;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Validator;
use support\Db;
use support\package\Auth\UserAuth;
use support\Request;
use support\Response;
use App\api\controller\Controller;
use think\Validate;

class Ping extends Controller
{

    public function getRules(): array
    {
        return [
            'index'=>[
                'rules'=>[
                    'name' => 'require|integer|>=:0',
                ],
                'message'=>[
                    'name.require' => '名字是必须传的,狗日的',
                ]
            ]
        ];
    }

    public function index(Request $request): Response
    {
        Db::beginTransaction();
        CollectionsWarehouse::query()->create([
            'user_id'=>'111',
            'serial_number'=>1,
            'collection_id'=>1,
            'gift_number'=>1,
            'term_date'=>time(),
            'update_time'=>time(),
        ]);
        Db::rollBack();
        return json(['11111']);
    }

}
1417 1 0
1个回答

shyrank

找到问题了,是model里面的这个参数问题 $fillable,会导致事务失效

  • sharf 2023-11-08

    Db::connection('mysql')->beginTransaction();

  • sharf 2023-11-08

    model里定义了连接 protected $connection = 'mysql';,需要 Db::connection('mysql')->beginTransaction();

年代过于久远,无法发表回答
🔝