Dav*_*vid 15 php events magento observer-pattern
我正在使用customer_save_aftermagento中的事件,除了1个烦人的事情之外,所有事情都很好 - 它总是被解雇两次.
没有其他模块重写这个,我找不到其他原因发生这种情况.当我查看此时被解雇的所有事件时,这个事件肯定会被解雇两次.
有谁解释这个?
我正在编写一个与此相关的Web服务,并且它的复制效果非常低效.
Jon*_*Day 23
我也注意到了这种双重保存行为.防止观察者出现问题的方法是在请求中设置一个可以检查的标志,例如
if(Mage::registry('customer_save_observer_executed')){
return $this; //this method has already been executed once in this request (see comment below)
}
...execute arbitrary code here....
/* Customer Addresses seem to call the before_save event twice,
* so we need to set a variable so we only process it once, otherwise we get duplicates
*/
Mage::register('customer_save_observer_executed',true);
Run Code Online (Sandbox Code Playgroud)
tha*_*smt 11
我也碰到了这个并且在观察者中为每个方法做了一个堆栈跟踪,并且至少可以告诉你为什么它会发射两次(可能还有其他的):
当新用户创建帐户时,createPostAction()会在提交表单时运行.此操作save()对客户起作用.
然后,在创建客户之后,createPostAction()调用setCustomerAsLoggedIn().这反过来调用setCustomer(),它有这么一点代码:
if ((!$customer->isConfirmationRequired()) && $customer->getConfirmation()) {
$customer->setConfirmation(null)->save(); // here is the second save
$customer->setIsJustConfirmed(true);
}
Run Code Online (Sandbox Code Playgroud)
这是两个save()s调度save事件.我对Magento 1.5中的帐户创建肯定知道这一点.我怀疑在管理区域中创建用户时是否会被触发两次,或者当用户编辑他们的信息时...但我不确定.
我希望这有帮助!