事务管理(Model Transactions)

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

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

自定义事务(Manual Transactions)

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

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

隐含的事务(Implicit Transactions)

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

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

单独的事务(Isolated Transactions)

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

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

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

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

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

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

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

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

当一个事务处于活动状态时,在整个应用中事务管理器将总是返回这个相同的事务。