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']);
}
}
找到问题了,是model里面的这个参数问题 $fillable,会导致事务失效
Db::connection('mysql')->beginTransaction();
model里定义了连接 protected $connection = 'mysql';,需要 Db::connection('mysql')->beginTransaction();