事务处理

 


 

开启事务

 

UserModel::startTrans();

 

提交事务

 

UserModel::commit();

 

回滚事务

 

UserModel::rollback();

 

<?php
namespace model\user;

use Timo\Orm\Model;

class UserModel extends Model
{
    
}

 

<?php
namespace business\user;

use model\user\UserModel;

class UserBusiness
{
    public function demoTransaction()
    {
        UserModel::startTrans();
        $ret = UserModel::where('uid', 10008)->update([
            'integral' => ['+', 10],
            'balance' => ['-', 100],
        ]);
        if ($ret === false) {
            UserModel::rollback();
            return App::result(1, '更新失败');
        }
        
        $ret = OrderModel::where('sn', '5498393485994')->update([
            'amount' => 1000,
            'num' => 10
        ]);
        if ($ret === false) {
            UserModel::rollback();
            return App::result(1, '更新失败');
        }
        UserModel::db()->commit();
        
        return App::result(0, '更新成功');
    }
}