事务处理

 


 

开启事务

 

$this->startTrans();

 

提交事务

 

$this->commit();

 

回滚事务

 

$this->rollback();

 

<?php
namespace common\model\user;

use common\model\WebModel;

class UserModel extends WebModel
{
    public function demoTransaction()
    {
        $this->startTrans();
        $data = array(
            'phone' => '13900008888',
            'integral' => ['+', 10],
            'balance' => ['-', 100],
        );
        $ret = $this->update($data, array('uid' => 10008));
        if ($ret === false) {
            $this->rollback();
            return $this->result(1, '更新失败');
        }
        $data = array(
            'amount' => 1000,
            'num' => 10
        );
        $ret = $orderModel->updateOrder($data);
        if ($ret === false) {
            $this->rollback();
            return $this->result(1, '更新失败');
        }
        $this->commit();
        return $this->result(0, '更新成功');
    }
}