You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: "Subscribing on events in Magento custom models"
4
+
date: 2016-07-14 17:00:00 +0400
5
+
comments: true
6
+
tags: magento magento-models
7
+
---
8
+
To subscribe on events only for your custom models in Magento you need to redefine `$_eventPrefix` property of your model like this:
9
+
10
+
```php
11
+
<?php
12
+
13
+
class Vendor_Module_Model_Something extends Mage_Core_Model_Abstract
14
+
{
15
+
protected $_eventPrefix = 'custom_event_prefix';
16
+
17
+
...
18
+
}
19
+
```
20
+
21
+
Then you can subscribe on your events in `etc/config.xml` file of your module as usual:
22
+
23
+
```xml
24
+
<?xml version="1.0"?>
25
+
<config>
26
+
27
+
...
28
+
29
+
<global>
30
+
31
+
...
32
+
33
+
<events>
34
+
<custom_event_prefix_save_after>
35
+
<observers>
36
+
<your_identifier>
37
+
<class>Vendor_Module_Model_Observer</class>
38
+
<method>yourMethodName</method>
39
+
</your_identifier>
40
+
</observers>
41
+
</custom_event_prefix_save_after>
42
+
</events>
43
+
44
+
...
45
+
46
+
</global>
47
+
48
+
...
49
+
50
+
</config>
51
+
```
52
+
53
+
### What model events are available?
54
+
55
+
Here is a list of available events for a model in Magento 1.9 (assuming `PREFIX` will be replaced by your prefix):
56
+
57
+
```
58
+
PREFIX_load_before
59
+
PREFIX_load_after
60
+
PREFIX_save_commit_after
61
+
PREFIX_save_before
62
+
PREFIX_save_after
63
+
PREFIX_delete_before
64
+
PREFIX_delete_after
65
+
PREFIX_delete_commit_after
66
+
PREFIX_clear
67
+
```
68
+
69
+
Explanation of some:
70
+
71
+
***PREFIX_save_commit_after** this event happens after save transaction commit in resource model
72
+
***PREFIX_delete_commit_after** happens after delete transaction commit in resource model
73
+
***PREFIX_clear** happens before clearing data in model's `clearInstance()` method. Rarely used
74
+
75
+
Other events are pretty obvious.
76
+
77
+
### What about standard models?
78
+
79
+
Magento standard models already have their own event prefixes. If you need them, just do a search for `$_eventPrefix = ` in `app/code/core/Mage` folder.
0 commit comments