美文网首页
thinkphp5.1使用事务

thinkphp5.1使用事务

作者: 吴涛涛 | 来源:发表于2020-04-27 16:21 被阅读0次

    1.事务和try...catch结合使用

    //开启事务
    DB::startTrans();
    try {
            //循环添加班级
            foreach ($classes_names as $name) {
            //添加
            ClassesModel::create(['grade_id'=>$params['grade_id'], 'name'=>$name, 'teacher_name'=>$params['teacher_name'], 'remark'=>$params['remark']]);
            }
            //这里也可以执行其他操作  比如:删除更新
            //
            //提交事务
            DB::commit();
            return json(['code'=>200,'status'=>'成功']);
    } catch (\Exception $e) {
            //回滚事务
            DB::rollback();
            return json(['code'=>400,'status'=>$e->getMessage()]);
    }
    

    相关文章

      网友评论

          本文标题:thinkphp5.1使用事务

          本文链接:https://www.haomeiwen.com/subject/asfuwhtx.html