/**
* 多库事务调用
* @param array $configName
* @param Closure $callback
* @return mixed
* @author 张志强
* @date 2024-04-15
*/
function transaction(array $configName, Closure $callback)
{
if (count($configName) > 0) {
$config = array_shift($configName);
$return = "";
Db::connect($config)->transaction(function () use ($callback, $configName, &$return) {
$return = transaction($configName, $callback);
});
return $return;
} else {
return $callback();
}
}
/**
* 添加
* @throws Exception
*/
public static function addDisabler(array $data): int
{
return transaction(["mysql"], function () use ($data) {
// 里面的逻辑 如果报错会自动回滚 没有报错会自动提交
$disablerModel = new Disabler();
$disablerModel->save($data);
DisablerImgModel::addImage(array_merge($data, ['disabler_id' => $disablerModel->id]));
return $disablerModel->id;
});
}
Thinkphp6的模型多库事务
于 2024-04-17 14:58:48 首次发布