00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 abstract class Mage_Core_Model_Abstract extends Varien_Object
00036 {
00037
00038
00039
00040
00041
00042 protected $_eventPrefix = 'core_abstract';
00043
00044
00045
00046
00047
00048
00049
00050
00051 protected $_eventObject = 'object';
00052
00053
00054
00055
00056
00057
00058 protected $_resourceName;
00059
00060
00061
00062
00063
00064
00065 protected $_resource;
00066
00067
00068
00069
00070
00071
00072 protected $_resourceCollectionName;
00073
00074
00075
00076
00077
00078
00079
00080
00081 protected $_cacheTag = false;
00082
00083
00084
00085
00086
00087
00088
00089
00090 protected $_dataSaveAllowed = true;
00091
00092
00093
00094
00095
00096
00097
00098
00099 protected function _init($resourceModel)
00100 {
00101 $this->_setResourceModel($resourceModel);
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 protected function _setResourceModel($resourceName, $resourceCollectionName=null)
00113 {
00114 $this->_resourceName = $resourceName;
00115 if (is_null($resourceCollectionName)) {
00116 $resourceCollectionName = $resourceName.'_collection';
00117 }
00118 $this->_resourceCollectionName = $resourceCollectionName;
00119 }
00120
00121
00122
00123
00124
00125
00126 protected function _getResource()
00127 {
00128 if (empty($this->_resourceName)) {
00129 Mage::throwException(Mage::helper('core')->__('Resource is not set'));
00130 }
00131
00132 return Mage::getResourceSingleton($this->_resourceName);
00133 }
00134
00135
00136
00137
00138
00139
00140
00141 public function getIdFieldName()
00142 {
00143 if (!($fieldName = parent::getIdFieldName())) {
00144 $fieldName = $this->_getResource()->getIdFieldName();
00145 $this->setIdFieldName($fieldName);
00146 }
00147 return $fieldName;
00148 }
00149
00150
00151
00152
00153
00154
00155 public function getId()
00156 {
00157 if ($fieldName = $this->getIdFieldName()) {
00158 return $this->_getData($fieldName);
00159 } else {
00160 return $this->_getData('id');
00161 }
00162 }
00163
00164
00165
00166
00167
00168
00169
00170 public function setId($id)
00171 {
00172 if ($this->getIdFieldName()) {
00173 $this->setData($this->getIdFieldName(), $id);
00174 } else {
00175 $this->setData('id', $id);
00176 }
00177 return $this;
00178 }
00179
00180
00181
00182
00183
00184
00185 public function getResourceName()
00186 {
00187 return $this->_resourceName;
00188 }
00189
00190
00191
00192
00193
00194
00195 public function getResourceCollection()
00196 {
00197 if (empty($this->_resourceCollectionName)) {
00198 Mage::throwException(Mage::helper('core')->__('Model collection resource name is not defined'));
00199 }
00200 return Mage::getResourceModel($this->_resourceCollectionName, $this->_getResource());
00201 }
00202
00203 public function getCollection()
00204 {
00205 return $this->getResourceCollection();
00206 }
00207
00208
00209
00210
00211
00212
00213
00214 public function load($id, $field=null)
00215 {
00216 $this->_getResource()->load($this, $id, $field);
00217 $this->_afterLoad();
00218 $this->setOrigData();
00219 return $this;
00220 }
00221
00222
00223
00224
00225
00226
00227 protected function _afterLoad()
00228 {
00229 Mage::dispatchEvent('model_load_after', array('object'=>$this));
00230 Mage::dispatchEvent($this->_eventPrefix.'_load_after', array($this->_eventObject=>$this));
00231 return $this;
00232 }
00233
00234 public function afterLoad()
00235 {
00236 $this->getResource()->afterLoad($this);
00237 $this->_afterLoad();
00238 }
00239
00240
00241
00242
00243
00244
00245 public function save()
00246 {
00247 $this->_getResource()->beginTransaction();
00248 try {
00249 $this->_beforeSave();
00250 if ($this->_dataSaveAllowed) {
00251 $this->_getResource()->save($this);
00252 $this->_afterSave();
00253 }
00254 $this->_getResource()->commit();
00255 }
00256 catch (Exception $e){
00257 $this->_getResource()->rollBack();
00258 throw $e;
00259 }
00260 return $this;
00261 }
00262
00263
00264
00265
00266
00267
00268 protected function _beforeSave()
00269 {
00270 Mage::dispatchEvent('model_save_before', array('object'=>$this));
00271 Mage::dispatchEvent($this->_eventPrefix.'_save_before', array($this->_eventObject=>$this));
00272 return $this;
00273 }
00274
00275
00276
00277
00278
00279
00280 protected function _afterSave()
00281 {
00282 if ($this->_cacheTag) {
00283 if ($this->_cacheTag === true) {
00284 $tags = array();
00285 }
00286 else {
00287 $tags = array($this->_cacheTag);
00288 }
00289 Mage::app()->cleanCache($tags);
00290 }
00291 Mage::dispatchEvent('model_save_after', array('object'=>$this));
00292 Mage::dispatchEvent($this->_eventPrefix.'_save_after', array($this->_eventObject=>$this));
00293 return $this;
00294 }
00295
00296
00297
00298
00299
00300
00301 public function delete()
00302 {
00303 $this->_getResource()->beginTransaction();
00304 try {
00305 $this->_beforeDelete();
00306 $this->_getResource()->delete($this);
00307 $this->_afterDelete();
00308
00309 $this->_getResource()->commit();
00310 }
00311 catch (Exception $e){
00312 $this->_getResource()->rollBack();
00313 throw $e;
00314 }
00315 return $this;
00316 }
00317
00318
00319
00320
00321
00322
00323 protected function _beforeDelete()
00324 {
00325 Mage::dispatchEvent('model_delete_before', array('object'=>$this));
00326 Mage::dispatchEvent($this->_eventPrefix.'_delete_before', array($this->_eventObject=>$this));
00327 return $this;
00328 }
00329
00330
00331
00332
00333
00334
00335 protected function _protectFromNonAdmin()
00336 {
00337 if (Mage::registry('isSecureArea')) {
00338 return;
00339 }
00340 if (!Mage::app()->getStore()->isAdmin()) {
00341 Mage::throwException(Mage::helper('core')->__('Cannot complete this operation from non-admin area.'));
00342 }
00343 }
00344
00345
00346
00347
00348
00349
00350 protected function _afterDelete()
00351 {
00352 if ($this->_cacheTag) {
00353 if ($this->_cacheTag === true) {
00354 $tags = array();
00355 }
00356 else {
00357 $tags = array($this->_cacheTag);
00358 }
00359 Mage::app()->cleanCache($tags);
00360 }
00361 Mage::dispatchEvent('model_delete_after', array('object'=>$this));
00362 Mage::dispatchEvent($this->_eventPrefix.'_delete_after', array($this->_eventObject=>$this));
00363 return $this;
00364 }
00365
00366
00367
00368
00369
00370
00371 public function getResource()
00372 {
00373 return $this->_getResource();
00374 }
00375
00376 public function getEntityId()
00377 {
00378 return $this->_getData('entity_id');
00379 }
00380 }