在我的插件Model/Observer.php
中,我有2个函数,它们对应于2个不同的事件,但我想将第一个函数的变量传递给下一个函数。我试过这样的事情:
class ContactologyProduct_LogProductUpdate_Model_Observer
{
(...)
public function beforeAddressSave(Varien_Event_Observer $observer) {
$contactologyClientIdBeforeEdit = $this->getCustomerAddress();
$this->setData('contactologyClientIdBeforeEdit', $contactologyClientIdBeforeEdit); // won't work
}
public function afterAddressSave(Varien_Event_Observer $observer) {
$contactologyClientIdAfterEdit = $this->getCustomerAddress();
$contactologyClientIdBeforeEdit = $this->getData('contactologyClientIdBeforeEdit'); // won't work
var_dump($contactologyClientIdBeforeEdit); // won't work
var_dump($contactologyClientIdAfterEdit);
}
}
但setData()
无法正常工作,我收到错误:
Call to a member function setData() on a non-object
在$contactologyClientIdBeforeEdit
函数中保存beforeAddressSave()
的方法是正确的,以便稍后可以在afterAddressSave()
中访问它?
答案 0 :(得分:0)
public function beforeAddressSave(Varien_Event_Observer $observer) {
$contactologyClientIdBeforeEdit = $this->getCustomerAddress();
$this->setData('contactologyClientIdBeforeEdit', $contactologyClientIdBeforeEdit); // won't work
}
你应该使用$ observer而不是$ this,但是你应该看看事件的创建地点,因为你可以有类似的东西:
$contactologyClientIdBeforeEdit = $observer->getEvent()->getCustomerAddress();
然后,您可以在类中实现setData和getData,或者让您的类扩展Varien_Object类。 所以,我想你会有:
class ContactologyProduct_LogProductUpdate_Model_Observer extends Varien_Object {
public function beforeAddressSave(Varien_Event_Observer $observer) {
$contactologyClientIdBeforeEdit = $observer->getCustomerAddress(); //or $observer->getEvent()->getCustomerAddress() look how the event data is set.
$this->setData('contactologyClientIdBeforeEdit', $contactologyClientIdBeforeEdit);
}
public function afterAddressSave(Varien_Event_Observer $observer) {
$contactologyClientIdAfterEdit = $observer->getCustomerAddress();
$contactologyClientIdBeforeEdit = $this->getData('contactologyClientIdBeforeEdit');
}