Event – How do I adjust my event observer in Magento?

My observer is not called. I want to know how to dispatch the event so that I can debug it.

< div class="answer"> This is a breakdown of Magento’s Mage_Core_Model_App::dispatchEvent() call, which is used to trigger events. My comment will be before the code I described is inserted. I will miss the obvious. It is worth mentioning that this is from the perspective of Magento Enterprise 1.10.1.1.

First of all, this is the entire method definition:

public function dispatchEvent($eventName, $args)
{
foreach ($this->_events as $area=>$events) {
if (!isset($events[$eventName ])) {
$eventConfig = $this->getConfig()->getEventConfig($area, $eventName);
if (!$eventConfig) {
$this->_events[ $area][$eventName] = false;
continue;
}
$observers = array();
foreach ($eventConfig->observers->children() as $ obsName=>$obsConfig) {
$observers[$obsName] = array(
'type' => (string)$obsConfig->type,
'model' => $obsConfig- >class? (st ring)$obsConfig->class: $obsConfig->getClassName(),
'method'=> (string)$obsConfig->method,
'args' => (array)$obsConfig-> args,
);
}
$events[$eventName]['observers'] = $observers;
$this->_events[$area][$eventName][ 'observers'] = $observers;
}
if (false===$events[$eventName]) {
continue;
} else {
$event = new Varien_Event($args);
$event->setName($eventName);
$observer = new Varien_Event_Observer();
}

foreach ($ events[$eventName]['observers'] as $obsName=>$obs) {
$observer->setData(array('event'=>$event));
Varien_Profiler::start( 'OBSERVER:'.$obsName);
switch ($obs['type']) {
case'disabled':
break;
case'object': c ase'model':
$method = $obs['method'];
$observer->addData($args);
$object = Mage::getModel($obs[' model']);
$this->_callObserverMethod($object, $method, $observer);
break;
default:
$method = $obs['method' ];
$observer->addData($args);
$object = Mage::getSingleton($obs['model']);
$this->_callObserverMethod($object, $method, $observer);
break;
}
Varien_Profiler::stop('OBSERVER:'.$obsName);
}
}
return $this;
}

We start with the declaration. An event with a specific name is triggered, and some optional supporting data and it.

< pre>public function dispatchEvent($eventName, $args)
{

Although some events have simple names, events such as predispatch and postdispatch events related to the controller are not only emitted General events, controller-specific events will also be emitted. Please wait a moment and see Mage_Core_Controller_Varien_Action The preDispatch method in:

Mage::dispatchEvent('controller_action_predispatch', array('controller_action'=>$this));
Mage::dispatchEvent(< br />'controller_action_predispatch_'.$this->getRequest()->getRouteName(),
array('controller_action'=>$this)
);
Mage::dispatchEvent(< br />'controller_action_predispatch_'.$this->getFullActionName(),
array('controller_action'=>$this)

Send three different events at the same time, you can use any of them Configure the observer on the above.

Next, we loop to view the event area and events registered by the observer. The event area can be “front-end” or “global”, but this is an internal one that you should not pay attention to Concept.

foreach ($this->_events as $area=>$events) {
if (!isset($events[$eventName])) {

Get the derived configuration of this event. This is essentially an XML node that contains all observers defined by the configuration for this event. Unless at least one observer is defined for this event, no one will be found Configuration. In this case, we set $this-> _events [$area] [$eventName] to FALSE, and we will bypass this check through the above conditions in the future.

$eventConfig = $this->getConfig()->getEventConfig($area, $eventName);
if (!$eventConfig) {
$this->_events[$area][$eventNam e] = false;
continue;
}

Build a set of registered observers.

$observers = array() ;
foreach ($eventConfig->observers->children() as $obsName=>$obsConfig) {
$observers[$obsName] = array(
'type' => (string )$obsConfig->type,
'model' => $obsConfig->class? (string)$obsConfig->class: $obsConfig->getClassName(),
'method'=> (string )$obsConfig->method,
'args' => (array)$obsConfig->args,
);
}
$events[$eventName]['observers' ] = $observers;
$this->_events[$area][$eventName]['observers'] = $observers;
}

If you pass here or before If no observer is found in the pass, then loop.

if (false===$events[$eventName]) {
continue;

Otherwise, prepare to trigger the observer. We built a Varien_Event as a very simple container for the event, and a Varien_Event_Observer to further wrap it. This is sent to the observer as a parameter.

< /p> } else {
$event = new Varien_Event($args);
$event->setName($eventName);
$observer = new Varien_Event_Observer();
}

Now, loop observers. For each observer, instantiate the observer’s object according to its defined type (“model”, singleton”, etc.). Then, call _callObserverMethod, which ensures that the This method exists before calling the object. Note that the type of observer “singleton” belongs to the following “default” case.

foreach ($events[$eventName]['observers' ] as $obsName=>$obs) {
$observer->setData(array('event'=>$event));
Varien_Profiler::start('OBSERVER:'.$obsName);
switch ($obs['type']) {
case'disabled':
break;
case'object': case'model':
$method = $obs['method'];
$observer->addData($args);
$object = Mage::getModel($obs['model']);
$this ->_callObserverMethod($object, $method, $observer);
break;
defa ult:
$method = $obs['method'];
$observer->addData($args);
$object = Mage::getSingleton($obs['model'] );
$this->_callObserverMethod($object, $method, $observer);
break;
}
Varien_Profiler::stop('OBSERVER:'.$obsName) ;
}
}
return $this;
}

My observer is not called. I want to know How to dispatch the event so that I can debug it.

This is a breakdown of Magento’s Mage_Core_Model_App::dispatchEvent() call, which is used to trigger the event. My comment will Before inserting the code I described. I will miss the obvious things. It is worth mentioning that this is from the perspective of Magento Enterprise 1.10.1.1.

First of all, this is the whole Method definition:

public function dispatchEvent($eventName, $args)
{
foreach ($this->_events as $area=>$events ) {
if (!isset($events[$eventName])) {
$eventConfig = $this->getConfig()->getEventConfig($area, $eventName);
if (!$eventConfig) {
$this->_events[$area][$ev entName] = false;
continue;
}
$observers = array();
foreach ($eventConfig->observers->children() as $obsName=>$obsConfig ) {
$observers[$obsName] = array(
'type' => (string)$obsConfig->type,
'model' => $obsConfig->class? (string )$obsConfig->class: $obsConfig->getClassName(),
'method'=> (string)$obsConfig->method,
'args' => (array)$obsConfig->args ,
);
}
$events[$eventName]['observers'] = $observers;
$this->_events[$area][$eventName][' observers'] = $observers;
}
if (false===$events[$eventName]) {
continue;
} else {
$event = new Varien_Event($args);
$event->setName($eventName);
$observer = new Varien_Event_Observer();
}

foreach ($events[$eventName]['observers'] as $obsName=>$obs) {
$observer->setData(array('event'=> $event));
Varien_Profiler::start('OBSERVER:'.$obsName);
switch ($obs['type']) {
case'disabled':
break;
case'object': case'model':
$method = $obs['method'];
$observer->addData($args);
$object = Mage::getModel($obs['model']);
$this->_callObserverMethod($object, $method, $observer);
break;
default:< br /> $method = $obs['method'];
$observer->addData($args);
$object = Mage::getSingleton($obs['model']);< br /> $this->_callObserverMethod($object, $method, $observer);
break;
}< br /> Varien_Profiler::stop('OBSERVER:'.$obsName);
}
}
return $this;
}

We start from the declaration Start. Triggered an event with a specific name, and some optional supporting data and it.

public function dispatchEvent($eventName, $args)
{

Although some events have simple names, events such as controller-related predispatch and postdispatch events will not only emit general events, but also controller-specific events. Please wait a moment and see The preDispatch method in Mage_Core_Controller_Varien_Action:

Mage::dispatchEvent('controller_action_predispatch', array('controller_action'=>$this));
Mage::dispatchEvent(
'controller_action_predispatch_'.$this->getRequest()->getRouteName(),
array('controller_action'=>$this)
);
Mage::dispatchEvent(
'controller_action_predispatch_'.$this->getFullActionName(),
array('controller_action'=>$this)

Send three different events at the same time, you can An upper configuration observer.

Next, we loop to view the event area and events that the observer has registered. The event area can be "front-end" or "global", but this is one you should not pay attention to Internal concept.

foreach ($this->_events as $area=>$events) {
if (!isse t($events[$eventName])) {

Get the derived configuration of this event. This is essentially an XML node that contains all observers defined for this event through configuration. Unless defined for this event At least one observer, otherwise no configuration will be found. In this case, we set $this-> _events [$area] [$eventName] to FALSE, and we will bypass this check by passing the above conditions in the future.

$eventConfig = $this->getConfig()->getEventConfig($area, $eventName);
if (!$eventConfig) {
$this ->_events[$area][$eventName] = false;
continue;
}

Build a set of registered observers.

$observers = array();
foreach ($eventConfig->observers->children() as $obsName=>$obsConfig) {
$observers[$obsName] = array(
'type' => (string)$obsConfig->type,
'model' => $obsConfig->class? (string)$obsConfig->class: $obsConfig->getClassName(),
'method'=> (string)$obsConfig->method,
'args' => (array)$obsConfig->args,
);
}
$ events[$eventName]['obse rvers'] = $observers;
$this->_events[$area][$eventName]['observers'] = $observers;
}

If you pass here Or the observer is not found in the previous pass, then loop.

if (false===$events[$eventName]) {
continue;

pre>

Otherwise, prepare to trigger the observer. We built a Varien_Event as a very simple container for the event, and a Varien_Event_Observer to further wrap it. This is sent to the observer as a parameter.

} else {
$event = new Varien_Event($args);
$event->setName($eventName);
$observer = new Varien_Event_Observer() ;
}

Now, loop observers. For each observer, instantiate the observer's object according to its defined type ("model", singleton", etc.). Then, call _ callObserverMethod, this ensures that the method exists before calling the object. Note that the type of observer "singleton" belongs to the following "default" case.

foreach ($events[$ eventName]['observers'] as $obsName=>$obs) {
$observer->setData(array('event'=>$event));
Varien_Profiler::start('OBSERVER: '.$obsName);
switch ($obs['type']) {
case'disabled':
break;
case'object': case'model':
$method = $obs['method'];
$observer->addData($args);
$object = Mage::getModel ($obs['model']);
$this->_callObserverMethod($object, $method, $observer);
break;
default:
$method = $ obs['method'];
$observer->addData($args);
$object = Mage::getSingleton($obs['model']);
$this-> _callObserverMethod($object, $method, $observer);
break;
}
Varien_Profiler::stop('OBSERVER:'.$obsName);
}
}
return $this;
}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4426 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.