1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: abstract class Mage_Core_Model_Abstract extends Varien_Object
36: {
37: 38: 39: 40: 41:
42: protected $_eventPrefix = 'core_abstract';
43:
44: 45: 46: 47: 48: 49: 50:
51: protected $_eventObject = 'object';
52:
53: 54: 55: 56: 57:
58: protected $_resourceName;
59:
60: 61: 62: 63: 64:
65: protected $_resource;
66:
67: 68: 69: 70: 71:
72: protected $_resourceCollectionName;
73:
74: 75: 76: 77: 78: 79: 80:
81: protected $_cacheTag = false;
82:
83: 84: 85: 86: 87: 88: 89:
90: protected $_dataSaveAllowed = true;
91:
92: 93: 94: 95: 96:
97: protected $_isObjectNew = null;
98:
99: 100: 101: 102: 103: 104: 105:
106: protected function _init($resourceModel)
107: {
108: $this->_setResourceModel($resourceModel);
109: }
110:
111: 112: 113: 114: 115: 116: 117: 118:
119: protected function _setResourceModel($resourceName, $resourceCollectionName=null)
120: {
121: $this->_resourceName = $resourceName;
122: if (is_null($resourceCollectionName)) {
123: $resourceCollectionName = $resourceName.'_collection';
124: }
125: $this->_resourceCollectionName = $resourceCollectionName;
126: }
127:
128: 129: 130: 131: 132:
133: protected function _getResource()
134: {
135: if (empty($this->_resourceName)) {
136: Mage::throwException(Mage::helper('core')->__('Resource is not set.'));
137: }
138:
139: return Mage::getResourceSingleton($this->_resourceName);
140: }
141:
142:
143: 144: 145: 146: 147:
148: public function getIdFieldName()
149: {
150: if (!($fieldName = parent::getIdFieldName())) {
151: $fieldName = $this->_getResource()->getIdFieldName();
152: $this->setIdFieldName($fieldName);
153: }
154: return $fieldName;
155: }
156:
157: 158: 159: 160: 161:
162: public function getId()
163: {
164: $fieldName = $this->getIdFieldName();
165: if ($fieldName) {
166: return $this->_getData($fieldName);
167: } else {
168: return $this->_getData('id');
169: }
170: }
171:
172: 173: 174: 175: 176: 177:
178: public function setId($id)
179: {
180: if ($this->getIdFieldName()) {
181: $this->setData($this->getIdFieldName(), $id);
182: } else {
183: $this->setData('id', $id);
184: }
185: return $this;
186: }
187:
188: 189: 190: 191: 192:
193: public function getResourceName()
194: {
195: return $this->_resourceName;
196: }
197:
198: 199: 200: 201: 202:
203: public function getResourceCollection()
204: {
205: if (empty($this->_resourceCollectionName)) {
206: Mage::throwException(Mage::helper('core')->__('Model collection resource name is not defined.'));
207: }
208: return Mage::getResourceModel($this->_resourceCollectionName, $this->_getResource());
209: }
210:
211: public function getCollection()
212: {
213: return $this->getResourceCollection();
214: }
215:
216: 217: 218: 219: 220: 221:
222: public function load($id, $field=null)
223: {
224: $this->_beforeLoad($id, $field);
225: $this->_getResource()->load($this, $id, $field);
226: $this->_afterLoad();
227: $this->setOrigData();
228: $this->_hasDataChanges = false;
229: return $this;
230: }
231:
232: 233: 234: 235: 236:
237: protected function _getEventData()
238: {
239: return array(
240: 'data_object' => $this,
241: $this->_eventObject => $this,
242: );
243: }
244:
245: 246: 247: 248: 249:
250: protected function _beforeLoad($id, $field = null)
251: {
252: $params = array('object' => $this, 'field' => $field, 'value'=> $id);
253: Mage::dispatchEvent('model_load_before', $params);
254: $params = array_merge($params, $this->_getEventData());
255: Mage::dispatchEvent($this->_eventPrefix.'_load_before', $params);
256: return $this;
257: }
258:
259: 260: 261: 262: 263:
264: protected function _afterLoad()
265: {
266: Mage::dispatchEvent('model_load_after', array('object'=>$this));
267: Mage::dispatchEvent($this->_eventPrefix.'_load_after', $this->_getEventData());
268: return $this;
269: }
270:
271:
272:
273: 274: 275: 276: 277:
278: public function afterLoad()
279: {
280: $this->getResource()->afterLoad($this);
281: $this->_afterLoad();
282: return $this;
283: }
284:
285: 286: 287: 288: 289: 290: 291:
292: protected function _hasModelChanged()
293: {
294: return $this->hasDataChanges();
295: }
296:
297: 298: 299: 300: 301:
302: public function save()
303: {
304: 305: 306:
307: if ($this->isDeleted()) {
308: return $this->delete();
309: }
310: if (!$this->_hasModelChanged()) {
311: return $this;
312: }
313: $this->_getResource()->beginTransaction();
314: $dataCommited = false;
315: try {
316: $this->_beforeSave();
317: if ($this->_dataSaveAllowed) {
318: $this->_getResource()->save($this);
319: $this->_afterSave();
320: }
321: $this->_getResource()->addCommitCallback(array($this, 'afterCommitCallback'))
322: ->commit();
323: $this->_hasDataChanges = false;
324: $dataCommited = true;
325: } catch (Exception $e) {
326: $this->_getResource()->rollBack();
327: $this->_hasDataChanges = true;
328: throw $e;
329: }
330: if ($dataCommited) {
331: $this->_afterSaveCommit();
332: }
333: return $this;
334: }
335:
336: 337: 338: 339: 340:
341: public function afterCommitCallback()
342: {
343: Mage::dispatchEvent('model_save_commit_after', array('object'=>$this));
344: Mage::dispatchEvent($this->_eventPrefix.'_save_commit_after', $this->_getEventData());
345: return $this;
346: }
347:
348: 349: 350: 351: 352: 353: 354:
355: protected function _afterSaveCommit()
356: {
357: return $this;
358: }
359:
360: 361: 362: 363: 364: 365: 366: 367: 368:
369: public function isObjectNew($flag=null)
370: {
371: if ($flag !== null) {
372: $this->_isObjectNew = $flag;
373: }
374: if ($this->_isObjectNew !== null) {
375: return $this->_isObjectNew;
376: }
377: return !(bool)$this->getId();
378: }
379:
380: 381: 382: 383: 384:
385: protected function _beforeSave()
386: {
387: if (!$this->getId()) {
388: $this->isObjectNew(true);
389: }
390: Mage::dispatchEvent('model_save_before', array('object'=>$this));
391: Mage::dispatchEvent($this->_eventPrefix.'_save_before', $this->_getEventData());
392: return $this;
393: }
394:
395: 396: 397: 398: 399: 400:
401: public function getCacheTags()
402: {
403: $tags = false;
404: if ($this->_cacheTag) {
405: if ($this->_cacheTag === true) {
406: $tags = array();
407: } else {
408: if (is_array($this->_cacheTag)) {
409: $tags = $this->_cacheTag;
410: } else {
411: $tags = array($this->_cacheTag);
412: }
413: $idTags = $this->getCacheIdTags();
414: if ($idTags) {
415: $tags = array_merge($tags, $idTags);
416: }
417: }
418: }
419: return $tags;
420: }
421:
422: 423: 424: 425: 426:
427: public function getCacheIdTags()
428: {
429: $tags = false;
430: if ($this->getId() && $this->_cacheTag) {
431: $tags = array();
432: if (is_array($this->_cacheTag)) {
433: foreach ($this->_cacheTag as $_tag) {
434: $tags[] = $_tag.'_'.$this->getId();
435: }
436: } else {
437: $tags[] = $this->_cacheTag.'_'.$this->getId();
438: }
439: }
440: return $tags;
441: }
442:
443: 444: 445: 446: 447:
448: public function cleanModelCache()
449: {
450: $tags = $this->getCacheTags();
451: if ($tags !== false) {
452: Mage::app()->cleanCache($tags);
453: }
454: return $this;
455: }
456:
457: 458: 459: 460: 461:
462: protected function _afterSave()
463: {
464: $this->cleanModelCache();
465: Mage::dispatchEvent('model_save_after', array('object'=>$this));
466: Mage::dispatchEvent($this->_eventPrefix.'_save_after', $this->_getEventData());
467: return $this;
468: }
469:
470: 471: 472: 473: 474:
475: public function delete()
476: {
477: $this->_getResource()->beginTransaction();
478: try {
479: $this->_beforeDelete();
480: $this->_getResource()->delete($this);
481: $this->_afterDelete();
482:
483: $this->_getResource()->commit();
484: $this->_afterDeleteCommit();
485: }
486: catch (Exception $e){
487: $this->_getResource()->rollBack();
488: throw $e;
489: }
490: return $this;
491: }
492:
493: 494: 495: 496: 497:
498: protected function _beforeDelete()
499: {
500: Mage::dispatchEvent('model_delete_before', array('object'=>$this));
501: Mage::dispatchEvent($this->_eventPrefix.'_delete_before', $this->_getEventData());
502: $this->cleanModelCache();
503: return $this;
504: }
505:
506: 507: 508: 509: 510:
511: protected function _protectFromNonAdmin()
512: {
513: if (Mage::registry('isSecureArea')) {
514: return;
515: }
516: if (!Mage::app()->getStore()->isAdmin()) {
517: Mage::throwException(Mage::helper('core')->__('Cannot complete this operation from non-admin area.'));
518: }
519: }
520:
521: 522: 523: 524: 525:
526: protected function _afterDelete()
527: {
528: Mage::dispatchEvent('model_delete_after', array('object'=>$this));
529: Mage::dispatchEvent($this->_eventPrefix.'_delete_after', $this->_getEventData());
530: return $this;
531: }
532:
533: 534: 535: 536: 537:
538: protected function _afterDeleteCommit()
539: {
540: Mage::dispatchEvent('model_delete_commit_after', array('object'=>$this));
541: Mage::dispatchEvent($this->_eventPrefix.'_delete_commit_after', $this->_getEventData());
542: return $this;
543: }
544:
545: 546: 547: 548: 549:
550: public function getResource()
551: {
552: return $this->_getResource();
553: }
554:
555: public function getEntityId()
556: {
557: return $this->_getData('entity_id');
558: }
559:
560: 561: 562: 563: 564:
565: final public function clearInstance()
566: {
567: $this->_clearReferences();
568: Mage::dispatchEvent($this->_eventPrefix.'_clear', $this->_getEventData());
569: $this->_clearData();
570: return $this;
571: }
572:
573: 574: 575: 576: 577:
578: protected function _clearReferences()
579: {
580: return $this;
581: }
582:
583: 584: 585: 586: 587:
588: protected function _clearData()
589: {
590: return $this;
591: }
592:
593: }
594: