/**
//事务调用
transaction($this->getModel(), function () use ( $v_data, $log_model) {
$res = $this->getModel()->setLog('编辑')->save($cl_log_data);
$res1 = $vehicle_model->setLog('编辑')->where('ref', $param['ref'])->update($v_data);
$ou_log_data['operate_id'] = $res['id'];
$log_model->setLog('编辑')->save($ou_log_data);
});
//全局
globalTransaction(function () use ($order, $vehicle, $historys, $deposit) {
$order->setLog('编辑')->save();//更新订单
$vehicle->setLog('编辑')->save();//更新车
$historys->setLog('编辑')->save();//更新车使用记录
$deposit->setLog('创建')->save();//保存押金记录
});
* 事务
* @param $model
* @param callable $callback
* @throws \app\Error
*/
function transaction(Model $model, callable $callback)
{
try {
$model->startTrans();
$callback();
$model->commit();
} catch (\Exception $e) {
$model->rollback();
Log::debug($e->getMessage() . $e->getFile() . $e->getLine());
throw new \app\Error($e->getMessage());
}
}
/**
* 全局事务
* @desc (全局事务)
* @param callable $callback
* @throws \app\Error
*/
function globalTransaction(callable $callback)
{
Db::startTrans();
try {
$callback();
Db::commit();
} catch (\Exception $e) {
Db::rollback();
Log::debug($e->getMessage() . $e->getFile() . $e->getLine());
throw new \app\Error($e->getMessage());
}
}