PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Core/Model/Resource/Abstract.php

https://bitbucket.org/jokusafet/magento2
PHP | 244 lines | 107 code | 16 blank | 121 comment | 22 complexity | d4af8473218cadf3294ce0e6c6a96df6 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Abstract resource model
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Core_Model_Resource_Abstract
  34. {
  35. /**
  36. * @var Varien_Db_Adapter_Interface
  37. */
  38. protected $_writeAdapter;
  39. /**
  40. * Main constructor
  41. */
  42. public function __construct()
  43. {
  44. /**
  45. * Please override this one instead of overriding real __construct constructor
  46. */
  47. $this->_construct();
  48. }
  49. /**
  50. * Array of callbacks subscribed to commit transaction commit
  51. *
  52. * @var array
  53. */
  54. static protected $_commitCallbacks = array();
  55. /**
  56. * Resource initialization
  57. */
  58. abstract protected function _construct();
  59. /**
  60. * Retrieve connection for read data
  61. */
  62. abstract protected function _getReadAdapter();
  63. /**
  64. * Retrieve connection for write data
  65. */
  66. abstract protected function _getWriteAdapter();
  67. /**
  68. * Start resource transaction
  69. *
  70. * @return Mage_Core_Model_Resource_Abstract
  71. */
  72. public function beginTransaction()
  73. {
  74. $this->_getWriteAdapter()->beginTransaction();
  75. return $this;
  76. }
  77. /**
  78. * Subscribe some callback to transaction commit
  79. *
  80. * @param callback $callback
  81. * @return Mage_Core_Model_Resource_Abstract
  82. */
  83. public function addCommitCallback($callback)
  84. {
  85. $adapterKey = spl_object_hash($this->_getWriteAdapter());
  86. self::$_commitCallbacks[$adapterKey][] = $callback;
  87. return $this;
  88. }
  89. /**
  90. * Commit resource transaction
  91. *
  92. * @return Mage_Core_Model_Resource_Abstract
  93. */
  94. public function commit()
  95. {
  96. $this->_getWriteAdapter()->commit();
  97. /**
  98. * Process after commit callbacks
  99. */
  100. if ($this->_getWriteAdapter()->getTransactionLevel() === 0) {
  101. $adapterKey = spl_object_hash($this->_getWriteAdapter());
  102. if (isset(self::$_commitCallbacks[$adapterKey])) {
  103. $callbacks = self::$_commitCallbacks[$adapterKey];
  104. self::$_commitCallbacks[$adapterKey] = array();
  105. foreach ($callbacks as $index => $callback) {
  106. call_user_func($callback);
  107. }
  108. }
  109. }
  110. return $this;
  111. }
  112. /**
  113. * Roll back resource transaction
  114. *
  115. * @return Mage_Core_Model_Resource_Abstract
  116. */
  117. public function rollBack()
  118. {
  119. $this->_getWriteAdapter()->rollBack();
  120. return $this;
  121. }
  122. /**
  123. * Format date to internal format
  124. *
  125. * @param string|Zend_Date $date
  126. * @param bool $includeTime
  127. * @return string
  128. */
  129. public function formatDate($date, $includeTime=true)
  130. {
  131. return Varien_Date::formatDate($date, $includeTime);
  132. }
  133. /**
  134. * Convert internal date to UNIX timestamp
  135. *
  136. * @param string $str
  137. * @return int
  138. */
  139. public function mktime($str)
  140. {
  141. return Varien_Date::toTimestamp($str);
  142. }
  143. /**
  144. * Serialize specified field in an object
  145. *
  146. * @param Varien_Object $object
  147. * @param string $field
  148. * @param mixed $defaultValue
  149. * @param bool $unsetEmpty
  150. * @return Mage_Core_Model_Resource_Abstract
  151. */
  152. protected function _serializeField(Varien_Object $object, $field, $defaultValue = null, $unsetEmpty = false)
  153. {
  154. $value = $object->getData($field);
  155. if (empty($value)) {
  156. if ($unsetEmpty) {
  157. $object->unsetData($field);
  158. } else {
  159. if (is_object($defaultValue) || is_array($defaultValue)) {
  160. $defaultValue = serialize($defaultValue);
  161. }
  162. $object->setData($field, $defaultValue);
  163. }
  164. } elseif (is_array($value) || is_object($value)) {
  165. $object->setData($field, serialize($value));
  166. }
  167. return $this;
  168. }
  169. /**
  170. * Unserialize Varien_Object field in an object
  171. *
  172. * @param Mage_Core_Model_Abstract $object
  173. * @param string $field
  174. * @param mixed $defaultValue
  175. */
  176. protected function _unserializeField(Varien_Object $object, $field, $defaultValue = null)
  177. {
  178. $value = $object->getData($field);
  179. if (empty($value)) {
  180. $object->setData($field, $defaultValue);
  181. } elseif (!is_array($value) && !is_object($value)) {
  182. $object->setData($field, unserialize($value));
  183. }
  184. }
  185. /**
  186. * Prepare data for passed table
  187. *
  188. * @param Varien_Object $object
  189. * @param string $table
  190. * @return array
  191. */
  192. protected function _prepareDataForTable(Varien_Object $object, $table)
  193. {
  194. $data = array();
  195. $fields = $this->_getWriteAdapter()->describeTable($table);
  196. foreach (array_keys($fields) as $field) {
  197. if ($object->hasData($field)) {
  198. $fieldValue = $object->getData($field);
  199. if ($fieldValue instanceof Zend_Db_Expr) {
  200. $data[$field] = $fieldValue;
  201. } else {
  202. if (null !== $fieldValue) {
  203. $fieldValue = $this->_prepareTableValueForSave($fieldValue, $fields[$field]['DATA_TYPE']);
  204. $data[$field] = $this->_getWriteAdapter()->prepareColumnValue($fields[$field], $fieldValue);
  205. } else if (!empty($fields[$field]['NULLABLE'])) {
  206. $data[$field] = null;
  207. }
  208. }
  209. }
  210. }
  211. return $data;
  212. }
  213. /**
  214. * Prepare value for save
  215. *
  216. * @param mixed $value
  217. * @param string $type
  218. * @return mixed
  219. */
  220. protected function _prepareTableValueForSave($value, $type)
  221. {
  222. $type = strtolower($type);
  223. if ($type == 'decimal' || $type == 'numeric' || $type == 'float') {
  224. $value = Mage::app()->getLocale()->getNumber($value);
  225. }
  226. return $value;
  227. }
  228. }