Thinkphp5事务处理疑问

浏览:394 发布日期:2018/03/13 分类:求助交流
做了一个选课系统学习下TP框架,下面是选择课程和学生删除已选课程的代码,但对于事务处理不是很理解,请各位大神看看我这个代码是不是该这样写,如果写的不对,那是哪里有错误,谢谢指正!public function upkc()
    {
        Db::transaction(function(){
            $cx['xh'] = Request::instance()->param('xh');
            $kcbh['kcbh'] = Request::instance()->param('kcbh');
            $sj = Db::table('student_cz')->where($cx)->field('sfxk,xb')->find();
            $kcxx = Db::table('teacher')->where($kcbh)->find();
            $data['xzkc1'] = $kcxx['kcmc'];
            $data['rkjs1'] = $kcxx['rkjs'];
            $data['kcbh1'] = $kcxx['kcbh'];
            $data['sfxk'] = 1;
            $data['xksj'] = date('Y-m-d H:i:s');
            Db::table('student_cz')->where($cx)->update($data);/*学生表更新信息*/
            Db::table('teacher')->where($kcbh)->setDec('syrs');/*教师表更新信息*/
            return 0;
        });
    }
public function delkc()
    {
        Db::transaction(function(){
            $cx['xh'] = Request::instance()->param('xh');
            $kcbh['kcbh'] = Request::instance()->param('kcbh');
            $data['xzkc'] = '';
            $data['rkjs'] = '';
            $data['kcbh'] = '';
            $data['sfxk'] = 0;
            Db::table('student')->where($cx)->update($data);/*删除学生表课程信息*/
            Db::table('teacher')->where($kcbh)->setInc('syrs');/*删除教师表课程信息*/
            return 0;
        });
    }
最佳答案
评论( 相关
后面还有条评论,点击查看>>