0

好的,所以我有一个观察者正在观察这个controller_action_postdispatch_customer_account_createpost动作。我的问题是,在我尝试执行以下操作的方法中:

public function customerSaveAfter($observer)
{
    /** @var Mage_Customer_Model_Customer $customer */
    $customer = $observer->getEvent()->getCustomer();
}

无论我做什么,$customer 都是 NULL。在此之前调用了另一个扩展,它以完全相同的方式使用该方法并提供了一个客户。请帮忙。

4

1 回答 1

2

客户对象是空白的,因为该controller_action_postdispatch_customer_account_createpost事件是控制器动作事件,与客户对象无关。该事件在以下代码中发出

#File: app/code/core/Mage/Core/Controller/Varien/Action.php
public function postDispatch()
{
    if ($this->getFlag('', self::FLAG_NO_POST_DISPATCH)) {
        return;
    }

    Mage::dispatchEvent(
        'controller_action_postdispatch_'.$this->getFullActionName(),
        array('controller_action'=>$this)
    );
    Mage::dispatchEvent(
        'controller_action_postdispatch_'.$this->getRequest()->getRouteName(),
        array('controller_action'=>$this)
    );
    Mage::dispatchEvent('controller_action_postdispatch', array('controller_action'=>$this));
}

具体来说,

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

少量。($this->getRequest()->getRouteName()返回customer_account_createpost)。请注意

array('controller_action'=>$this)

被传递到事件调度中——这意味着您可以使用以下命令从观察者访问控制器对象

$observer->getControllerAction();
$observer->getData('controller_action');

您还可以使用以下命令获取观察者的数据键变量列表

var_dump(
    array_keys($observer->getData())
);

“其他扩展”(我假设您的意思是另一个扩展的观察者对象)可能正在侦听不同的事件,一个将customer对象传递给事件的事件。例如,考虑customer_login事件。

#File: app/code/core/Customer/Model/Session.php
public function setCustomerAsLoggedIn($customer)
{
    $this->setCustomer($customer);
    Mage::dispatchEvent('customer_login', array('customer'=>$customer));
    return $this;
}

这里的事件派发包括一个客户对象

array('customer'=>$customer)

这意味着客户将在您的观察者中可用。

于 2013-05-07T21:46:34.290 回答