PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/xampp/htdocs/magento/app/code/core/Mage/Core/Model/Abstract.php

https://github.com/edmondscommerce/XAMPP-Magento-Demo-Site
PHP | 380 lines | 179 code | 32 blank | 169 comment | 17 complexity | f7b9d0557c0eb971101dd71d11229ae9 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. * If collection name is ommited, resource name will be used with _collection appended
  98. *
  99. * @param string $resourceName
  100. * @param string|null $resourceCollectionName
  101. */
  102. protected function _setResourceModel($resourceName, $resourceCollectionName=null)
  103. {
  104. $this->_resourceName = $resourceName;
  105. if (is_null($resourceCollectionName)) {
  106. $resourceCollectionName = $resourceName.'_collection';
  107. }
  108. $this->_resourceCollectionName = $resourceCollectionName;
  109. }
  110. /**
  111. * Get resource instance
  112. *
  113. * @return Mage_Core_Model_Mysql4_Abstract
  114. */
  115. protected function _getResource()
  116. {
  117. if (empty($this->_resourceName)) {
  118. Mage::throwException(Mage::helper('core')->__('Resource is not set'));
  119. }
  120. return Mage::getResourceSingleton($this->_resourceName);
  121. }
  122. /**
  123. * Retrieve identifier field name for model
  124. *
  125. * @return string
  126. */
  127. public function getIdFieldName()
  128. {
  129. if (!($fieldName = parent::getIdFieldName())) {
  130. $fieldName = $this->_getResource()->getIdFieldName();
  131. $this->setIdFieldName($fieldName);
  132. }
  133. return $fieldName;
  134. }
  135. /**
  136. * Retrieve model object identifier
  137. *
  138. * @return mixed
  139. */
  140. public function getId()
  141. {
  142. if ($fieldName = $this->getIdFieldName()) {
  143. return $this->_getData($fieldName);
  144. } else {
  145. return $this->_getData('id');
  146. }
  147. }
  148. /**
  149. * Declare model object identifier value
  150. *
  151. * @param mixed $id
  152. * @return Mage_Core_Model_Abstract
  153. */
  154. public function setId($id)
  155. {
  156. if ($this->getIdFieldName()) {
  157. $this->setData($this->getIdFieldName(), $id);
  158. } else {
  159. $this->setData('id', $id);
  160. }
  161. return $this;
  162. }
  163. /**
  164. * Retrieve model resource name
  165. *
  166. * @return string
  167. */
  168. public function getResourceName()
  169. {
  170. return $this->_resourceName;
  171. }
  172. /**
  173. * Get collection instance
  174. *
  175. * @return object
  176. */
  177. public function getResourceCollection()
  178. {
  179. if (empty($this->_resourceCollectionName)) {
  180. Mage::throwException(Mage::helper('core')->__('Model collection resource name is not defined'));
  181. }
  182. return Mage::getResourceModel($this->_resourceCollectionName, $this->_getResource());
  183. }
  184. public function getCollection()
  185. {
  186. return $this->getResourceCollection();
  187. }
  188. /**
  189. * Load object data
  190. *
  191. * @param integer $id
  192. * @return Mage_Core_Model_Abstract
  193. */
  194. public function load($id, $field=null)
  195. {
  196. $this->_getResource()->load($this, $id, $field);
  197. $this->_afterLoad();
  198. $this->setOrigData();
  199. return $this;
  200. }
  201. /**
  202. * Processing object after load data
  203. *
  204. * @return Mage_Core_Model_Abstract
  205. */
  206. protected function _afterLoad()
  207. {
  208. Mage::dispatchEvent('model_load_after', array('object'=>$this));
  209. Mage::dispatchEvent($this->_eventPrefix.'_load_after', array($this->_eventObject=>$this));
  210. return $this;
  211. }
  212. public function afterLoad()
  213. {
  214. $this->getResource()->afterLoad($this);
  215. $this->_afterLoad();
  216. }
  217. /**
  218. * Save object data
  219. *
  220. * @return Mage_Core_Model_Abstract
  221. */
  222. public function save()
  223. {
  224. $this->_getResource()->beginTransaction();
  225. try {
  226. $this->_beforeSave();
  227. if ($this->_dataSaveAllowed) {
  228. $this->_getResource()->save($this);
  229. $this->_afterSave();
  230. }
  231. $this->_getResource()->commit();
  232. }
  233. catch (Exception $e){
  234. $this->_getResource()->rollBack();
  235. throw $e;
  236. }
  237. return $this;
  238. }
  239. /**
  240. * Processing object before save data
  241. *
  242. * @return Mage_Core_Model_Abstract
  243. */
  244. protected function _beforeSave()
  245. {
  246. Mage::dispatchEvent('model_save_before', array('object'=>$this));
  247. Mage::dispatchEvent($this->_eventPrefix.'_save_before', array($this->_eventObject=>$this));
  248. return $this;
  249. }
  250. /**
  251. * Processing object after save data
  252. *
  253. * @return Mage_Core_Model_Abstract
  254. */
  255. protected function _afterSave()
  256. {
  257. if ($this->_cacheTag) {
  258. if ($this->_cacheTag === true) {
  259. $tags = array();
  260. }
  261. else {
  262. $tags = array($this->_cacheTag);
  263. }
  264. Mage::app()->cleanCache($tags);
  265. }
  266. Mage::dispatchEvent('model_save_after', array('object'=>$this));
  267. Mage::dispatchEvent($this->_eventPrefix.'_save_after', array($this->_eventObject=>$this));
  268. return $this;
  269. }
  270. /**
  271. * Delete object from database
  272. *
  273. * @return Mage_Core_Model_Abstract
  274. */
  275. public function delete()
  276. {
  277. $this->_getResource()->beginTransaction();
  278. try {
  279. $this->_beforeDelete();
  280. $this->_getResource()->delete($this);
  281. $this->_afterDelete();
  282. $this->_getResource()->commit();
  283. }
  284. catch (Exception $e){
  285. $this->_getResource()->rollBack();
  286. throw $e;
  287. }
  288. return $this;
  289. }
  290. /**
  291. * Processing object before delete data
  292. *
  293. * @return Mage_Core_Model_Abstract
  294. */
  295. protected function _beforeDelete()
  296. {
  297. Mage::dispatchEvent('model_delete_before', array('object'=>$this));
  298. Mage::dispatchEvent($this->_eventPrefix.'_delete_before', array($this->_eventObject=>$this));
  299. return $this;
  300. }
  301. /**
  302. * Safeguard func that will check, if we are in admin area
  303. *
  304. * @throws Mage_Core_Exception
  305. */
  306. protected function _protectFromNonAdmin()
  307. {
  308. if (Mage::registry('isSecureArea')) {
  309. return;
  310. }
  311. if (!Mage::app()->getStore()->isAdmin()) {
  312. Mage::throwException(Mage::helper('core')->__('Cannot complete this operation from non-admin area.'));
  313. }
  314. }
  315. /**
  316. * Processing object after delete data
  317. *
  318. * @return Mage_Core_Model_Abstract
  319. */
  320. protected function _afterDelete()
  321. {
  322. if ($this->_cacheTag) {
  323. if ($this->_cacheTag === true) {
  324. $tags = array();
  325. }
  326. else {
  327. $tags = array($this->_cacheTag);
  328. }
  329. Mage::app()->cleanCache($tags);
  330. }
  331. Mage::dispatchEvent('model_delete_after', array('object'=>$this));
  332. Mage::dispatchEvent($this->_eventPrefix.'_delete_after', array($this->_eventObject=>$this));
  333. return $this;
  334. }
  335. /**
  336. * Retrieve model resource
  337. *
  338. * @return Mage_Core_Model_Mysql4_Abstract
  339. */
  340. public function getResource()
  341. {
  342. return $this->_getResource();
  343. }
  344. public function getEntityId()
  345. {
  346. return $this->_getData('entity_id');
  347. }
  348. }