TP5 事务操作


 // 启动事务
            Db::startTrans();
            try {
                $save = [
                    'wx'=>$data['wx'],
                    'nickname'=>$data['nickname'],
                    'sex'=>$data['sex'],
                    'birth_date'=>$data['birth_date'],
                    'email'=>$data['email'],
                    'address'=>$data['address']
                ];
                $ok = Db::name('User')->where(['id'=>$user_id])->update($save);
                if($ok!==false){
                    //自定义字段保存
                    $field_save = model('api/v1/User')->field_list_save($data,$user_id);
                    if($field_save['code']!=200){
                        throw new \Exception($field_save['msg']);
                    }
                    //获取职位所属分类id
                    $category = explode('/',$job['category']);
                    $tid = $category?end($category):'';
                    $message_data = [
                        'tid'=>$tid,
                        'pid'=>$data['id'],
                        'add_user_id'=>$user_id
                    ];
                    $insert = Db::name('JobMessage')->insert($message_data);
                    if($insert){
                        $json = ['code'=>200,'msg'=>'申请成功,请等待后台审核'];
                    }else{
                        throw new \Exception('申请失败,请联系管理员');
                    }
                }
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $json = ['code'=>-200,'msg'=>$e->getMessage()];
            }
            return $json;
 

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM