事务处理


开启事务

User::connection()->startTrans();

提交事务

User::connection()->commit();

回滚事务

User::connection()->rollback();

举例

  1. <?php
  2. namespace model\user;
  3. use Timo\Orm\Model;
  4. class User extends Model
  5. {
  6. }
  1. <?php
  2. namespace business\user;
  3. use model\user\User;
  4. class UserBusiness
  5. {
  6. public function demoTransaction()
  7. {
  8. User::connection()->startTrans();
  9. $ret = User::where('uid', 10008)->update([
  10. 'integral' => ['+', 10],
  11. 'balance' => ['-', 100],
  12. ]);
  13. if ($ret === false) {
  14. User::connection()->rollback();
  15. return App::result(1, '更新失败');
  16. }
  17. $ret = Order::where('sn', '5498393485994')->update([
  18. 'amount' => 1000,
  19. 'num' => 10
  20. ]);
  21. if ($ret === false) {
  22. User::connection()->rollback();
  23. return App::result(1, '更新失败');
  24. }
  25. User::connection()->commit();
  26. return App::result(0, '更新成功');
  27. }
  28. }