本文整理汇总了PHP中think\Model::commit方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::commit方法的具体用法?PHP Model::commit怎么用?PHP Model::commit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类think\Model
的用法示例。
在下文中一共展示了Model::commit方法的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' => '购买成功');
}
示例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;
// 用户名已存在
}
}
示例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;
// 用户名已存在
}
}
示例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;
}
示例5: commit
public function commit()
{
parent::commit();
$this->query('SET FOREIGN_KEY_CHECKS = 1');
}
示例6: 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();
}
}
示例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;
}
}
示例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);
}
}
}