模型事务(Model Transactions)

当一个进程执行多个数据库操作时,通常需要每一步都是成功完成以便保证数据完整性。事务可以确保在数据提交到数据库保存之前所有数据库操作都成功执行。

Phalcon中通过事务,可以在所有操作都成功执行之后提交到服务器,或者当有错误发生时回滚所有的操作。

自定义事务(Manual Transactions)

如果一个应用只用到了一个数据库连接并且这些事务都不太复杂,那么可以通过简单的将当前数据库连接设置成事务模式实现事务功能,根据操作的成功与否提交或者回滚:

  1. <?php
  2.  
  3. use Phalcon\Mvc\Controller;
  4.  
  5. class RobotsController extends Controller
  6. {
  7. public function saveAction()
  8. {
  9. // Start a transaction
  10. $this->db->begin();
  11.  
  12. $robot = new Robots();
  13. $robot->name = "WALL·E";
  14. $robot->created_at = date("Y-m-d");
  15.  
  16. // The model failed to save, so rollback the transaction
  17. if ($robot->save() == false) {
  18. $this->db->rollback();
  19. return;
  20. }
  21.  
  22. $robotPart = new RobotParts();
  23. $robotPart->robots_id = $robot->id;
  24. $robotPart->type = "head";
  25.  
  26. // The model failed to save, so rollback the transaction
  27. if ($robotPart->save() == false) {
  28. $this->db->rollback();
  29. return;
  30. }
  31.  
  32. // Commit the transaction
  33. $this->db->commit();
  34. }
  35. }

隐含的事务(Implicit Transactions)

也可以通过已有的关系来存储记录以及其相关记录,这种操作将隐式的创建一个事务来保证所有数据能够正确的保存:

  1. <?php
  2.  
  3. $robotPart = new RobotParts();
  4. $robotPart->type = "head";
  5.  
  6. $robot = new Robots();
  7. $robot->name = "WALL·E";
  8. $robot->created_at = date("Y-m-d");
  9. $robot->robotPart = $robotPart;
  10.  
  11. $robot->save(); // Creates an implicit transaction to store both records

单独的事务(Isolated Transactions)

单独事务在一个新的连接中执行所有的SQL,虚拟外键检查和业务规则与主数据库连接是相互独立的。这种事务需要一个事务管理器来全局的管理每一个事务,保证他们在请求结束前能正确的回滚或者提交。

  1. <?php
  2.  
  3. use Phalcon\Mvc\Model\Transaction\Failed as TxFailed;
  4. use Phalcon\Mvc\Model\Transaction\Manager as TxManager;
  5.  
  6. try {
  7.  
  8. // Create a transaction manager
  9. $manager = new TxManager();
  10.  
  11. // Request a transaction
  12. $transaction = $manager->get();
  13.  
  14. $robot = new Robots();
  15. $robot->setTransaction($transaction);
  16. $robot->name = "WALL·E";
  17. $robot->created_at = date("Y-m-d");
  18. if ($robot->save() == false) {
  19. $transaction->rollback("Cannot save robot");
  20. }
  21.  
  22. $robotPart = new RobotParts();
  23. $robotPart->setTransaction($transaction);
  24. $robotPart->robots_id = $robot->id;
  25. $robotPart->type = "head";
  26. if ($robotPart->save() == false) {
  27. $transaction->rollback("Cannot save robot part");
  28. }
  29.  
  30. // Everything's gone fine, let's commit the transaction
  31. $transaction->commit();
  32.  
  33. } catch (TxFailed $e) {
  34. echo "Failed, reason: ", $e->getMessage();
  35. }

事务可以用以保证以一致性的方式删除多条记录:

  1. <?php
  2.  
  3. use Phalcon\Mvc\Model\Transaction\Failed as TxFailed;
  4. use Phalcon\Mvc\Model\Transaction\Manager as TxManager;
  5.  
  6. try {
  7.  
  8. // Create a transaction manager
  9. $manager = new TxManager();
  10.  
  11. // Request a transaction
  12. $transaction = $manager->get();
  13.  
  14. // Get the robots to be deleted
  15. foreach (Robots::find("type = 'mechanical'") as $robot) {
  16. $robot->setTransaction($transaction);
  17. if ($robot->delete() == false) {
  18. // Something's gone wrong, we should rollback the transaction
  19. foreach ($robot->getMessages() as $message) {
  20. $transaction->rollback($message->getMessage());
  21. }
  22. }
  23. }
  24.  
  25. // Everything's gone fine, let's commit the transaction
  26. $transaction->commit();
  27.  
  28. echo "Robots were deleted successfully!";
  29.  
  30. } catch (TxFailed $e) {
  31. echo "Failed, reason: ", $e->getMessage();
  32. }

事务对象可以重用,不管事务对象是在什么地方获取的。只有当一个commit()或者一个rollback()执行时才会创建一个新的事务对象。可以通过服务容器在整个应用中来创建和管理全局师傅管理器。

  1. <?php
  2.  
  3. use Phalcon\Mvc\Model\Transaction\Manager as TransactionManager
  4.  
  5. $di->setShared('transactions', function () {
  6. return new TransactionManager();
  7. });

然后在控制器或者视图中访问:

  1. <?php
  2.  
  3. use Phalcon\Mvc\Controller;
  4.  
  5. class ProductsController extends Controller
  6. {
  7. public function saveAction()
  8. {
  9. // Obtain the TransactionsManager from the services container
  10. $manager = $this->di->getTransactions();
  11.  
  12. // Or
  13. $manager = $this->transactions;
  14.  
  15. // Request a transaction
  16. $transaction = $manager->get();
  17.  
  18. // ...
  19. }
  20. }

While a transaction is active, the transaction manager will always return the same transaction across the application.

原文: http://www.myleftstudio.com/reference/models-transactions.html