當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Model::startTrans方法代碼示例

本文整理匯總了PHP中think\Model::startTrans方法的典型用法代碼示例。如果您正苦於以下問題:PHP Model::startTrans方法的具體用法?PHP Model::startTrans怎麽用?PHP Model::startTrans使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在think\Model的用法示例。


在下文中一共展示了Model::startTrans方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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('world_user')->where("username = '{$username}'")->find();
     if ($result == null) {
         $user_info = array('username' => $username, 'password' => $data['password'], 'session' => md5(time() . $username), 'modify_time' => time(), 'last_time' => time());
         $uid = $tranDb->table('world_user')->data($user_info)->add();
         $tranDb->commit();
         return 1;
         // 注冊用戶成功
     } else {
         return -1;
         // 用戶名已存在
     }
 }
開發者ID:xinliuruoxi,項目名稱:Ones-World,代碼行數:22,代碼來源:UserModel.class.php

示例3: 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

示例4: 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

示例5: startTrans

 public function startTrans()
 {
     $this->query('SET FOREIGN_KEY_CHECKS = 0');
     parent::startTrans();
 }
開發者ID:npk,項目名稱:ones,代碼行數:5,代碼來源:CommonModel.class.php

示例6: begin

 /**
  * 啟動事務
  * @access public
  * @return void
  */
 public function begin($tag = 'default')
 {
     if (self::$_startedTrans && self::$_startedTrans != $tag) {
         return;
     }
     self::$_startedTrans = $tag;
     parent::startTrans();
 }
開發者ID:admpub,項目名稱:OpenCenter,代碼行數:13,代碼來源:Base.class.php

示例7: 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

示例8: 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::startTrans方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。