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

https://github.com/ronseigel/agent-ohm · PHP · 374 lines · 176 code · 32 blank · 166 comment · 16 complexity · a7090d6ef12ce852b428becfc2b3a747 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) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Abstract model class
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Core_Model_Abstract extends Varien_Object
  34. {
  35. /**
  36. * Prefix of model events names
  37. *
  38. * @var string
  39. */
  40. protected $_eventPrefix = 'core_abstract';
  41. /**
  42. * Parameter name in event
  43. *
  44. * In observe method you can use $observer->getEvent()->getObject() in this case
  45. *
  46. * @var string
  47. */
  48. protected $_eventObject = 'object';
  49. /**
  50. * Name of the resource model
  51. *
  52. * @var string
  53. */
  54. protected $_resourceName;
  55. /**
  56. * Resource model instance
  57. *
  58. * @var Mage_Core_Model_Mysql4_Abstract
  59. */
  60. protected $_resource;
  61. /**
  62. * Name of the resource collection model
  63. *
  64. * @var string
  65. */
  66. protected $_resourceCollectionName;
  67. /**
  68. * Model cache tag for clear cache in after save and after delete
  69. *
  70. * When you use true - all cache will be clean
  71. *
  72. * @var string || true
  73. */
  74. protected $_cacheTag = false;
  75. /**
  76. * Flag which can stop data saving after before save
  77. * Can be used for next sequence: we check data in _beforeSave, if data are
  78. * not valid - we can set this flag to false value and save process will be stopped
  79. *
  80. * @var bool
  81. */
  82. protected $_dataSaveAllowed = true;
  83. /**
  84. * Standard model initialization
  85. *
  86. * @param string $resourceModel
  87. * @param string $idFieldName
  88. * @return Mage_Core_Model_Abstract
  89. */
  90. protected function _init($resourceModel)
  91. {
  92. $this->_setResourceModel($resourceModel);
  93. }
  94. /**
  95. * Set resource names
  96. *
  97. * Resource name will be used with _collection appended
  98. *
  99. * @param string $resourceName
  100. */
  101. protected function _setResourceModel($resourceName)
  102. {
  103. $this->_resourceName = $resourceName;
  104. $this->_resourceCollectionName = $resourceName.'_collection';
  105. }
  106. /**
  107. * Get resource instance
  108. *
  109. * @return Mage_Core_Model_Mysql4_Abstract
  110. */
  111. protected function _getResource()
  112. {
  113. if (empty($this->_resourceName)) {
  114. AO::throwException(AO::helper('core')->__('Resource is not set'));
  115. }
  116. return AO::getResourceSingleton($this->_resourceName);
  117. }
  118. /**
  119. * Retrieve identifier field name for model
  120. *
  121. * @return string
  122. */
  123. public function getIdFieldName()
  124. {
  125. if (!($fieldName = parent::getIdFieldName())) {
  126. $fieldName = $this->_getResource()->getIdFieldName();
  127. $this->setIdFieldName($fieldName);
  128. }
  129. return $fieldName;
  130. }
  131. /**
  132. * Retrieve model object identifier
  133. *
  134. * @return mixed
  135. */
  136. public function getId()
  137. {
  138. if ($fieldName = $this->getIdFieldName()) {
  139. return $this->_getData($fieldName);
  140. } else {
  141. return $this->_getData('id');
  142. }
  143. }
  144. /**
  145. * Declare model object identifier value
  146. *
  147. * @param mixed $id
  148. * @return Mage_Core_Model_Abstract
  149. */
  150. public function setId($id)
  151. {
  152. if ($this->getIdFieldName()) {
  153. $this->setData($this->getIdFieldName(), $id);
  154. } else {
  155. $this->setData('id', $id);
  156. }
  157. return $this;
  158. }
  159. /**
  160. * Retrieve model resource name
  161. *
  162. * @return string
  163. */
  164. public function getResourceName()
  165. {
  166. return $this->_resourceName;
  167. }
  168. /**
  169. * Get collection instance
  170. *
  171. * @return object
  172. */
  173. public function getResourceCollection()
  174. {
  175. if (empty($this->_resourceCollectionName)) {
  176. AO::throwException(AO::helper('core')->__('Model collection resource name is not defined'));
  177. }
  178. return AO::getResourceModel($this->_resourceCollectionName, $this->_getResource());
  179. }
  180. public function getCollection()
  181. {
  182. return $this->getResourceCollection();
  183. }
  184. /**
  185. * Load object data
  186. *
  187. * @param integer $id
  188. * @return Mage_Core_Model_Abstract
  189. */
  190. public function load($id, $field=null)
  191. {
  192. $this->_getResource()->load($this, $id, $field);
  193. $this->_afterLoad();
  194. $this->setOrigData();
  195. return $this;
  196. }
  197. /**
  198. * Processing object after load data
  199. *
  200. * @return Mage_Core_Model_Abstract
  201. */
  202. protected function _afterLoad()
  203. {
  204. AO::dispatchEvent('model_load_after', array('object'=>$this));
  205. AO::dispatchEvent($this->_eventPrefix.'_load_after', array($this->_eventObject=>$this));
  206. return $this;
  207. }
  208. public function afterLoad()
  209. {
  210. $this->getResource()->afterLoad($this);
  211. $this->_afterLoad();
  212. }
  213. /**
  214. * Save object data
  215. *
  216. * @return Mage_Core_Model_Abstract
  217. */
  218. public function save()
  219. {
  220. $this->_getResource()->beginTransaction();
  221. try {
  222. $this->_beforeSave();
  223. if ($this->_dataSaveAllowed) {
  224. $this->_getResource()->save($this);
  225. $this->_afterSave();
  226. }
  227. $this->_getResource()->commit();
  228. }
  229. catch (Exception $e){
  230. $this->_getResource()->rollBack();
  231. throw $e;
  232. }
  233. return $this;
  234. }
  235. /**
  236. * Processing object before save data
  237. *
  238. * @return Mage_Core_Model_Abstract
  239. */
  240. protected function _beforeSave()
  241. {
  242. AO::dispatchEvent('model_save_before', array('object'=>$this));
  243. AO::dispatchEvent($this->_eventPrefix.'_save_before', array($this->_eventObject=>$this));
  244. return $this;
  245. }
  246. /**
  247. * Processing object after save data
  248. *
  249. * @return Mage_Core_Model_Abstract
  250. */
  251. protected function _afterSave()
  252. {
  253. if ($this->_cacheTag) {
  254. if ($this->_cacheTag === true) {
  255. $tags = array();
  256. }
  257. else {
  258. $tags = array($this->_cacheTag);
  259. }
  260. AO::app()->cleanCache($tags);
  261. }
  262. AO::dispatchEvent('model_save_after', array('object'=>$this));
  263. AO::dispatchEvent($this->_eventPrefix.'_save_after', array($this->_eventObject=>$this));
  264. return $this;
  265. }
  266. /**
  267. * Delete object from database
  268. *
  269. * @return Mage_Core_Model_Abstract
  270. */
  271. public function delete()
  272. {
  273. $this->_getResource()->beginTransaction();
  274. try {
  275. $this->_beforeDelete();
  276. $this->_getResource()->delete($this);
  277. $this->_afterDelete();
  278. $this->_getResource()->commit();
  279. }
  280. catch (Exception $e){
  281. $this->_getResource()->rollBack();
  282. throw $e;
  283. }
  284. return $this;
  285. }
  286. /**
  287. * Processing object before delete data
  288. *
  289. * @return Mage_Core_Model_Abstract
  290. */
  291. protected function _beforeDelete()
  292. {
  293. AO::dispatchEvent('model_delete_before', array('object'=>$this));
  294. AO::dispatchEvent($this->_eventPrefix.'_delete_before', array($this->_eventObject=>$this));
  295. return $this;
  296. }
  297. /**
  298. * Safeguard func that will check, if we are in admin area
  299. *
  300. * @throws Mage_Core_Exception
  301. */
  302. protected function _protectFromNonAdmin()
  303. {
  304. if (AO::registry('isSecureArea')) {
  305. return;
  306. }
  307. if (!AO::app()->getStore()->isAdmin()) {
  308. AO::throwException(AO::helper('core')->__('Cannot complete this operation from non-admin area.'));
  309. }
  310. }
  311. /**
  312. * Processing object after delete data
  313. *
  314. * @return Mage_Core_Model_Abstract
  315. */
  316. protected function _afterDelete()
  317. {
  318. if ($this->_cacheTag) {
  319. if ($this->_cacheTag === true) {
  320. $tags = array();
  321. }
  322. else {
  323. $tags = array($this->_cacheTag);
  324. }
  325. AO::app()->cleanCache($tags);
  326. }
  327. AO::dispatchEvent('model_delete_after', array('object'=>$this));
  328. AO::dispatchEvent($this->_eventPrefix.'_delete_after', array($this->_eventObject=>$this));
  329. return $this;
  330. }
  331. /**
  332. * Retrieve model resource
  333. */
  334. public function getResource()
  335. {
  336. return $this->_getResource();
  337. }
  338. public function getEntityId()
  339. {
  340. return $this->_getData('entity_id');
  341. }
  342. }