当前位置: 首页>>代码示例>>PHP>>正文


PHP Model::rollback方法代码示例

本文整理汇总了PHP中think\Model::rollback方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::rollback方法的具体用法?PHP Model::rollback怎么用?PHP Model::rollback使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在think\Model的用法示例。


在下文中一共展示了Model::rollback方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: buy

 /**
  * 生成订单
  * @param array $data 购买数据
  * @return array $ret 结果状态
  */
 public function buy($data)
 {
     //--开始事务
     $model = new Model();
     $model->startTrans();
     //--判断用户是否有足够余额
     $goods = D('goods')->getById($data['id']);
     date_default_timezone_set("Asia/Shanghai");
     $amount = $data['number'] * $goods['univalent'];
     $user = D('user')->getById(session('user')['id']);
     if ($user['balance'] < $amount) {
         return array('ret' => false, 'msg' => '余额不足');
     }
     //--修改用户余额
     $newBalance = $user['balance'] - $amount;
     if (!D('user')->where(['id' => $user['id']])->save(['balance' => $newBalance])) {
         $model->rollback();
     }
     //--创建订单
     $order->user_id = session('user')['id'];
     $order->goods_id = $goods['id'];
     $order->shop_id = D('Goods')->getById($goods['id'])['shop_id'];
     $order->goods_name = $goods['name'];
     $order->univalent = $goods['univalent'];
     $order->number = $data['number'];
     $order->amount = $data['number'] * $goods['univalent'];
     $order->remark = $data['remark'];
     if ($this->create($order)) {
         if (!($orderId = $this->add())) {
             $model->rollback();
             return array('ret' => false, 'msg' => $this->getError());
         }
     } else {
         return array('ret' => false, 'msg' => $this->getError());
     }
     //--创建资金变动
     $fundChange['user_id'] = $user['id'];
     $fundChange['associate_id'] = $orderId;
     $fundChange['time'] = time();
     $fundChange['type'] = '订单扣款';
     $fundChange['amount'] = $amount;
     $fundChange['before_change'] = $user['balance'];
     $fundChange['after_change'] = $user['balance'] - $amount;
     if (D('FundChange')->create($fundChange)) {
         if (!D('FundChange')->add()) {
             $model->rollback();
             return array('ret' => false, 'msg' => D('FundChange')->getError());
         }
     } else {
         return array('ret' => false, 'msg' => D('FundChange')->getError());
     }
     $model->commit();
     return array('ret' => true, 'msg' => '购买成功');
 }
开发者ID:qiao1994,项目名称:graduation-project,代码行数:59,代码来源:OrderModel.class.php

示例2: regist

 /**
  * 注册
  * @param array $data 注册数据
  */
 public function regist($data)
 {
     // 将用户信息添加到User表中
     $tranDb = new Model();
     $tranDb->startTrans();
     $username = $data['username'];
     $result = $tranDb->table('west_user')->where("username = '{$username}'")->find();
     if ($result == null) {
         $user_info = array('username' => $username, 'password' => $data['password'], 'group' => 'user', 'status' => 0, 'modify_time' => time());
         $uid = $tranDb->table('west_user')->data($user_info)->add();
         if ($uid) {
             // 判断公司是否存在
             $company_name = $data['company_name'];
             $result = $tranDb->table('west_company')->where("name = '{$company_name}'")->find();
             if ($result == null) {
                 $company_info = array('uid' => $user_id, 'name' => $data['company_name'], 'mail' => $data['company_mail'], 'tel' => $data['company_tel'], 'modify_time' => time());
                 $result = $tranDb->table('west_company')->data($company_info)->add();
                 if ($result) {
                     $tranDb->commit();
                     return 1;
                     // 注册用户成功
                 } else {
                     $tranDb->rollback();
                     return -4;
                     // 添加公司信息失败
                 }
             } else {
                 $tranDb->rollback();
                 return -3;
                 // 公司名称已存在
             }
         } else {
             $tranDb->rollback();
             return -2;
             // 添加用户信息失败
         }
     } else {
         return -1;
         // 用户名已存在
     }
 }
开发者ID:Yin-Far,项目名称:west,代码行数:45,代码来源:UserModel.class.php

示例3: create

 /**
  * 生成订单
  * @param $order
  * @param $goods
  * @return array
  */
 public function create($order, $goods)
 {
     $db = new Model();
     $db->startTrans();
     $order_id = $db->table($this->_table)->add($order);
     $status = false;
     if ($order_id) {
         foreach ($goods as $g) {
             $g['order_id'] = $order_id;
             $flag = $db->table($this->_table_order_goods)->add($g);
         }
         if ($flag) {
             $db->commit();
             $status = true;
         }
     }
     if (!$status) {
         $db->rollback();
     }
     return $order_id;
 }
开发者ID:xswolf,项目名称:dc,代码行数:27,代码来源:OrderModel.class.php

示例4: rollback

 public function rollback()
 {
     parent::rollback();
     $this->query('SET FOREIGN_KEY_CHECKS = 1');
 }
开发者ID:npk,项目名称:ones,代码行数:5,代码来源:CommonModel.class.php

示例5: end

 /**
  * 提交事务
  * @access public
  * @return boolean
  */
 public function end($success = true, $tag = 'default')
 {
     if ($success) {
         if (self::$_startedTrans != $tag) {
             return;
         }
         self::$_startedTrans = null;
         return parent::commit();
     } else {
         self::$_startedTrans = null;
         return parent::rollback();
     }
 }
开发者ID:admpub,项目名称:OpenCenter,代码行数:18,代码来源:Base.class.php

示例6: calculateRice

 public function calculateRice($rice_id)
 {
     $tranDb = new Think\Model();
     $tranDb->startTrans();
     //启动事务,保证数据的一致性
     $ricedata = $this->where("rice_id='{$rice_id}'")->find();
     $calculatedData = $this->calculateIndicator($ricedata);
     $rid = $this->where("rice_id='{$rice_id}'")->save($calculatedData);
     $wave = new InfoWaveModel();
     $wid = $wave->addWave($calculatedData);
     if ($rid !== false && $wid) {
         $tranDb->commit();
         return true;
     } else {
         $this->error = "计算失败!";
         $tranDb->rollback();
         return false;
     }
 }
开发者ID:RockChao,项目名称:RiceFinal,代码行数:19,代码来源:InfoRiceModel.class.php

示例7: EditSave

 /**
  *修改用户
  */
 public function EditSave()
 {
     //获取参数
     $id = I('id', 0, 'int');
     $username = I('username');
     $password = I('password');
     $email = I('email');
     $mobile = I('mobile');
     $roleid = I('role_id');
     //是否修改
     if ($id == 0) {
         $result['result'] = false;
         $result['msg'] = '该用户不存在';
         $this->ajaxReturn($result);
     }
     //开启事务
     $model = new Model();
     $model->startTrans();
     $flage === false;
     //申明修改用户对象
     $data['UserName'] = $username;
     if (!empty($password)) {
         $data['PassWord'] = md5($password);
     }
     $data['Email'] = $email;
     $data['Mobile'] = $mobile;
     //更新
     $flage = $model->table('sys_user')->where('id=%d', $id)->save($data);
     if ($flage === false) {
         //事务回滚
         $model->rollback();
         $result['result'] = false;
         $result['msg'] = '修改用户资料失败!';
         $this->ajaxReturn($result);
     } else {
         $flage = $model->table('sys_role_user')->where('user_id=%d', $id)->delete();
         if ($flage === false) {
             //事务回滚
             $model->rollback();
             $result['result'] = false;
             $result['msg'] = '修改用户资料失败!';
             $this->ajaxReturn($result);
         }
         //角色处理
         foreach ($roleid as $key => $value) {
             # code...
             $data2['role_id'] = $value;
             $data2['user_id'] = $id;
             $flage = $model->table('sys_role_user')->add($data2);
             if ($flage === false) {
                 //事务回滚
                 $model->rollback();
                 $result['result'] = false;
                 $result['msg'] = '修改用户资料失败!';
                 $this->ajaxReturn($result);
             }
         }
         if ($flage) {
             //事务提交
             $model->commit();
             $result['result'] = true;
             $result['msg'] = '修改用户资料成功!';
             $this->ajaxReturn($result);
         }
     }
 }
开发者ID:beyond1983,项目名称:Blogs,代码行数:69,代码来源:SysuserController.class.php


注:本文中的think\Model::rollback方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。