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, '更新成功');
}
}