transaction.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. /**
  13. * Interact with Database Transactions
  14. *
  15. * @author five07
  16. * @copyright (c) 2011 five07
  17. * @license https://github.com/five07/Fuel-Addons/blob/master/LICENSE
  18. */
  19. namespace Fuel\Core;
  20. /**
  21. * @deprecated remove in v1.2
  22. */
  23. class Database_Transaction
  24. {
  25. /**
  26. * @var Database_Transaction for Singleton-like usage
  27. */
  28. protected static $_instance = null;
  29. /**
  30. * @var \Fuel\Core\Database_Connection
  31. */
  32. protected $_db;
  33. public static function instance()
  34. {
  35. if (static::$_instance == null)
  36. {
  37. static::$_instance = static::forge();
  38. }
  39. return static::$_instance;
  40. }
  41. /**
  42. * Creates a new instance
  43. *
  44. * @param string $instance
  45. */
  46. public static function forge($instance = null)
  47. {
  48. logger(\Fuel::L_WARNING, 'The Database_Transaction class is deprecated, use the connection driver methods instead.', __METHOD__);
  49. return new static($instance);
  50. }
  51. /**
  52. * The constructor
  53. *
  54. * @param string $instance
  55. */
  56. public function __construct($instance = null)
  57. {
  58. $this->_db = Database_Connection::instance($instance);
  59. }
  60. /**
  61. * Start your transaction before a set of dependent queries
  62. */
  63. public function start()
  64. {
  65. $this->_db->start_transaction();
  66. }
  67. /**
  68. * Complete your transaction on the set of queries
  69. */
  70. public function complete()
  71. {
  72. try
  73. {
  74. static::commit();
  75. }
  76. catch (\Exception $e)
  77. {
  78. static::rollback();
  79. }
  80. }
  81. /**
  82. * If the group of queries had no errors, this returns TRUE
  83. * Otherwise, will return FALSE
  84. *
  85. * @return boolean
  86. */
  87. public function status()
  88. {
  89. return true;
  90. }
  91. /**
  92. * Commit the successful queries and reset AUTOCOMMIT
  93. * This is called automatically if you use Database_Transaction::complete()
  94. * It can also be used manually for testing
  95. */
  96. public function commit()
  97. {
  98. $this->_db->commit_transaction();
  99. }
  100. /**
  101. * Rollback the failed queries and reset AUTOCOMMIT
  102. * This is called automatically if you use Database_Transaction::complete()
  103. * It can also be used manually for testing
  104. */
  105. public function rollback()
  106. {
  107. $this->_db->rollback_transaction();
  108. }
  109. /**
  110. * Return the database errors
  111. *
  112. * @return mixed (array or false)
  113. */
  114. public function errors()
  115. {
  116. return false;
  117. }
  118. }