事务处理
开启事务
User::connection()->startTrans();
提交事务
User::connection()->commit();
回滚事务
User::connection()->rollback();
举例
<?php
namespace model\user;
use Timo\Orm\Model;
class User extends Model
{
}
<?php
namespace business\user;
use model\user\User;
class UserBusiness
{
public function demoTransaction()
{
User::connection()->startTrans();
$ret = User::where('uid', 10008)->update([
'integral' => ['+', 10],
'balance' => ['-', 100],
]);
if ($ret === false) {
User::connection()->rollback();
return App::result(1, '更新失败');
}
$ret = Order::where('sn', '5498393485994')->update([
'amount' => 1000,
'num' => 10
]);
if ($ret === false) {
User::connection()->rollback();
return App::result(1, '更新失败');
}
User::connection()->commit();
return App::result(0, '更新成功');
}
}