forked from magento/magento2
/
Abstract.php
556 lines (509 loc) · 14.4 KB
/
Abstract.php
1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Core
* @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Abstract model class
*
* @category Mage
* @package Mage_Core
* @author Magento Core Team <core@magentocommerce.com>
*/
abstract class Mage_Core_Model_Abstract extends Varien_Object
{
/**
* Prefix of model events names
*
* @var string
*/
protected $_eventPrefix = 'core_abstract';
/**
* Parameter name in event
*
* In observe method you can use $observer->getEvent()->getObject() in this case
*
* @var string
*/
protected $_eventObject = 'object';
/**
* Name of the resource model
*
* @var string
*/
protected $_resourceName;
/**
* Resource model instance
*
* @var Mage_Core_Model_Resource_Db_Abstract
*/
protected $_resource;
/**
* Name of the resource collection model
*
* @var string
*/
protected $_resourceCollectionName;
/**
* Model cache tag for clear cache in after save and after delete
*
* When you use true - all cache will be clean
*
* @var string || true
*/
protected $_cacheTag = false;
/**
* Flag which can stop data saving after before save
* Can be used for next sequence: we check data in _beforeSave, if data are
* not valid - we can set this flag to false value and save process will be stopped
*
* @var bool
*/
protected $_dataSaveAllowed = true;
/**
* Flag which allow detect object state: is it new object (without id) or existing one (with id)
*
* @var bool
*/
protected $_isObjectNew = null;
/**
* Constructor
*
* By default is looking for first argument as array and assigns it as object attributes
* This behavior may change in child classes
*/
public function __construct(array $data= array())
{
parent::__construct($data);
$this->_construct();
}
/**
* Model construct that should be used for object initialization
*/
protected function _construct()
{
}
/**
* Standard model initialization
*
* @param string $resourceModel
* @param string $idFieldName
* @return Mage_Core_Model_Abstract
*/
protected function _init($resourceModel)
{
$this->_setResourceModel($resourceModel);
$this->_idFieldName = $this->_getResource()->getIdFieldName();
}
/**
* Set resource names
*
* If collection name is ommited, resource name will be used with _collection appended
*
* @param string $resourceName
* @param string|null $resourceCollectionName
*/
protected function _setResourceModel($resourceName, $resourceCollectionName = null)
{
$this->_resourceName = $resourceName;
if (is_null($resourceCollectionName)) {
$resourceCollectionName = $resourceName . '_Collection';
}
$this->_resourceCollectionName = $resourceCollectionName;
}
/**
* Get resource instance
*
* @return Mage_Core_Model_Resource_Db_Abstract
*/
protected function _getResource()
{
if (empty($this->_resourceName)) {
Mage::throwException(Mage::helper('Mage_Core_Helper_Data')->__('Resource is not set.'));
}
return Mage::getResourceSingleton($this->_resourceName);
}
/**
* Retrieve model resource name
*
* @return string
*/
public function getResourceName()
{
return $this->_resourceName;
}
/**
* Get collection instance
*
* @return object
*/
public function getResourceCollection()
{
if (empty($this->_resourceCollectionName)) {
Mage::throwException(Mage::helper('Mage_Core_Helper_Data')->__('Model collection resource name is not defined.'));
}
return Mage::getResourceModel($this->_resourceCollectionName, $this->_getResource());
}
public function getCollection()
{
return $this->getResourceCollection();
}
/**
* Load object data
*
* @param integer $id
* @return Mage_Core_Model_Abstract
*/
public function load($id, $field=null)
{
$this->_beforeLoad($id, $field);
$this->_getResource()->load($this, $id, $field);
$this->_afterLoad();
$this->setOrigData();
$this->_hasDataChanges = false;
return $this;
}
/**
* Get array of objects transfered to default events processing
*
* @return array
*/
protected function _getEventData()
{
return array(
'data_object' => $this,
$this->_eventObject => $this,
);
}
/**
* Processing object before load data
*
* @return Mage_Core_Model_Abstract
*/
protected function _beforeLoad($id, $field = null)
{
$params = array('object' => $this, 'field' => $field, 'value'=> $id);
Mage::dispatchEvent('model_load_before', $params);
$params = array_merge($params, $this->_getEventData());
Mage::dispatchEvent($this->_eventPrefix.'_load_before', $params);
return $this;
}
/**
* Processing object after load data
*
* @return Mage_Core_Model_Abstract
*/
protected function _afterLoad()
{
Mage::dispatchEvent('model_load_after', array('object'=>$this));
Mage::dispatchEvent($this->_eventPrefix.'_load_after', $this->_getEventData());
return $this;
}
/**
* Object after load processing. Implemented as public interface for supporting objects after load in collections
*
* @return Mage_Core_Model_Abstract
*/
public function afterLoad()
{
$this->getResource()->afterLoad($this);
$this->_afterLoad();
return $this;
}
/**
* Check whether model has changed data.
* Can be overloaded in child classes to perform advanced check whether model needs to be saved
* e.g. usign resouceModel->hasDataChanged() or any other technique
*
* @return boolean
*/
protected function _hasModelChanged()
{
return $this->hasDataChanges();
}
/**
* Save object data
*
* @return Mage_Core_Model_Abstract
*/
public function save()
{
/**
* Direct deleted items to delete method
*/
if ($this->isDeleted()) {
return $this->delete();
}
if (!$this->_hasModelChanged()) {
return $this;
}
$this->_getResource()->beginTransaction();
$dataCommited = false;
try {
$this->_beforeSave();
if ($this->_dataSaveAllowed) {
$this->_getResource()->save($this);
$this->_afterSave();
}
$this->_getResource()->addCommitCallback(array($this, 'afterCommitCallback'))
->commit();
$this->_hasDataChanges = false;
$dataCommited = true;
} catch (Exception $e) {
$this->_getResource()->rollBack();
$this->_hasDataChanges = true;
throw $e;
}
if ($dataCommited) {
$this->afterCommitCallback();
}
return $this;
}
/**
* Callback function which called after transaction commit in resource model
*
* @return Mage_Core_Model_Abstract
*/
public function afterCommitCallback()
{
Mage::dispatchEvent('model_save_commit_after', array('object'=>$this));
Mage::dispatchEvent($this->_eventPrefix.'_save_commit_after', $this->_getEventData());
return $this;
}
/**
* Check object state (true - if it is object without id on object just created)
* This method can help detect if object just created in _afterSave method
* problem is what in after save onject has id and we can't detect what object was
* created in this transaction
*
* @param bool $flag
* @return bool
*/
public function isObjectNew($flag=null)
{
if ($flag !== null) {
$this->_isObjectNew = $flag;
}
if ($this->_isObjectNew !== null) {
return $this->_isObjectNew;
}
return !(bool)$this->getId();
}
/**
* Processing object before save data
*
* @return Mage_Core_Model_Abstract
*/
protected function _beforeSave()
{
if (!$this->getId()) {
$this->isObjectNew(true);
}
Mage::dispatchEvent('model_save_before', array('object'=>$this));
Mage::dispatchEvent($this->_eventPrefix.'_save_before', $this->_getEventData());
return $this;
}
/**
* Get list of cache tags applied to model object.
* Return false if cache tags are not supported by model
*
* @return array | false
*/
public function getCacheTags()
{
$tags = false;
if ($this->_cacheTag) {
if ($this->_cacheTag === true) {
$tags = array();
} else {
if (is_array($this->_cacheTag)) {
$tags = $this->_cacheTag;
} else {
$tags = array($this->_cacheTag);
}
$idTags = $this->getCacheIdTags();
if ($idTags) {
$tags = array_merge($tags, $idTags);
}
}
}
return $tags;
}
/**
* Get cahce tags associated with object id
*
* @return array
*/
public function getCacheIdTags()
{
$tags = false;
if ($this->getId() && $this->_cacheTag) {
$tags = array();
if (is_array($this->_cacheTag)) {
foreach ($this->_cacheTag as $_tag) {
$tags[] = $_tag.'_'.$this->getId();
}
} else {
$tags[] = $this->_cacheTag.'_'.$this->getId();
}
}
return $tags;
}
/**
* Remove model onject related cache
*
* @return Mage_Core_Model_Abstract
*/
public function cleanModelCache()
{
$tags = $this->getCacheTags();
if ($tags !== false) {
Mage::app()->cleanCache($tags);
}
return $this;
}
/**
* Processing object after save data
*
* @return Mage_Core_Model_Abstract
*/
protected function _afterSave()
{
$this->cleanModelCache();
Mage::dispatchEvent('model_save_after', array('object'=>$this));
Mage::dispatchEvent($this->_eventPrefix.'_save_after', $this->_getEventData());
return $this;
}
/**
* Delete object from database
*
* @return Mage_Core_Model_Abstract
*/
public function delete()
{
$this->_getResource()->beginTransaction();
try {
$this->_beforeDelete();
$this->_getResource()->delete($this);
$this->_afterDelete();
$this->_getResource()->commit();
$this->_afterDeleteCommit();
}
catch (Exception $e){
$this->_getResource()->rollBack();
throw $e;
}
return $this;
}
/**
* Processing object before delete data
*
* @return Mage_Core_Model_Abstract
*/
protected function _beforeDelete()
{
Mage::dispatchEvent('model_delete_before', array('object'=>$this));
Mage::dispatchEvent($this->_eventPrefix.'_delete_before', $this->_getEventData());
$this->cleanModelCache();
return $this;
}
/**
* Safeguard func that will check, if we are in admin area
*
* @throws Mage_Core_Exception
*/
protected function _protectFromNonAdmin()
{
if (Mage::registry('isSecureArea')) {
return;
}
if (!Mage::app()->getStore()->isAdmin()) {
Mage::throwException(Mage::helper('Mage_Core_Helper_Data')->__('Cannot complete this operation from non-admin area.'));
}
}
/**
* Processing object after delete data
*
* @return Mage_Core_Model_Abstract
*/
protected function _afterDelete()
{
Mage::dispatchEvent('model_delete_after', array('object'=>$this));
Mage::dispatchEvent($this->_eventPrefix.'_delete_after', $this->_getEventData());
return $this;
}
/**
* Processing manipulation after main transaction commit
*
* @return Mage_Core_Model_Abstract
*/
protected function _afterDeleteCommit()
{
Mage::dispatchEvent('model_delete_commit_after', array('object'=>$this));
Mage::dispatchEvent($this->_eventPrefix.'_delete_commit_after', $this->_getEventData());
return $this;
}
/**
* Retrieve model resource
*
* @return Mage_Core_Model_Resource_Db_Abstract
*/
public function getResource()
{
return $this->_getResource();
}
public function getEntityId()
{
return $this->_getData('entity_id');
}
/**
* Clearing object for correct deleting by garbage collector
*
* @return Mage_Core_Model_Abstract
*/
final public function clearInstance()
{
$this->_clearReferences();
Mage::dispatchEvent($this->_eventPrefix.'_clear', $this->_getEventData());
$this->_clearData();
return $this;
}
/**
* Clearing cyclic references
*
* @return Mage_Core_Model_Abstract
*/
protected function _clearReferences()
{
return $this;
}
/**
* Clearing object's data
*
* @return Mage_Core_Model_Abstract
*/
protected function _clearData()
{
return $this;
}
}