Magento backend_model - 我需要为每个配置字段指定吗?
如果我想在保存自定义模块的特定配置字段时做一些额外的事情(除了保存到 Magento 核心配置表之外),我可以在我的 system.xml 中为该字段指定一个 backend_model ,并拥有该后端模型类扩展 Mage_Core_Model_Config_Data,覆盖 _afterSave,并将我的额外内容放入该方法中。
但是如果我想为多个领域执行此操作该怎么办?我不希望行为是保存 field1 并为该字段调用我的 afterSave,保存 field2 并为该字段调用我的 afterSave 等等。我宁愿将所有字段保存到 Magento 核心配置表中,并且然后我做我的额外的事情。
这可能吗?我想我也许可以使用事件/观察者来实现这一点。所以在我的 config.xml 中,
<events>
<admin_system_config_changed_mysection>
<observers>
<mypfx_admin_system_config_changed_mysection>
<class>mymodule/adminhtml_system_config_backend_configSaveObserver</class>
<method>myConfigSaved</method
</mypfx_admin_system_config_changed_mysection>
</observers>
</admin_system_config_changed_mysection>
</events>
但是保存配置时不会调用我的观察者方法。也许我的活动名称有误?我猜测事件名称末尾的“mysection”位必须与 system.xml 中的部分相匹配:
<sections>
<mysection translate="label" module="mymodule">
...
<groups>
...
</groups>
</mysection>
</sections>
谢谢。
If I want to do something extra when a particular configuration field for my custom module is saved (over and above saving to the Magento core config table), I can just specify a backend_model for that field in my system.xml, and have that backend model class extend Mage_Core_Model_Config_Data, override _afterSave, and put my extra stuff in that method.
But what if I have several fields I want to do this for. I don't want the behaviour to be to save field1 and call my afterSave for that field, save field2 and call my afterSave for that field, etc. I'd rather that all the fields were saved to the Magento core config table, and then I do my extra stuff.
Is that possible? I thought I might be able to achieve that using event/observer. So in my config.xml, <adminhtml> section, I added an observer as follows:
<events>
<admin_system_config_changed_mysection>
<observers>
<mypfx_admin_system_config_changed_mysection>
<class>mymodule/adminhtml_system_config_backend_configSaveObserver</class>
<method>myConfigSaved</method
</mypfx_admin_system_config_changed_mysection>
</observers>
</admin_system_config_changed_mysection>
</events>
but my observer method is not called when the config is saved. Maybe I have the wrong event name? The "mysection" bit on the end of the event name I was guessing had to match the section from system.xml:
<sections>
<mysection translate="label" module="mymodule">
...
<groups>
...
</groups>
</mysection>
</sections>
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尝试侦听的事件不存在。以下是您想要做的事情,以及未来选择正确活动的一些提示。
首先,Magento 中的每个事件都是由
Mage::dispatchEvent
方法触发的。搜索这些调用的核心代码,您将始终知道要侦听的事件的名称。从上面的内容中,您可以看到事件的名称与您认为的名称的对比。
因此,您似乎在自己的部分名称之前缺少
部分
。其次,在开发盒子上工作时,找到您要查找的事件的最佳方法是在源头记录事件。暂时在
dispatchEvent
函数中添加一些调试代码。这会将大量事件名称转储到您的日志中。我通常使用 OS X 的
Console.app
在请求期间查看日志文件,复制行,排序并删除重复项,然后最终得到这样的列表您仍然需要使用一些智能猜测以确定您想要哪个活动,但它们的命名足够直观,您通常可以找到您想要的内容。
The event you're trying to listen for doesn't exist. Here's what you want to do, and some tips for picking the right event in the future.
First, every event is fired in Magento by the
Mage::dispatchEvent
method. Search the core code for these calls and you'll always know the name of the event you want to listen for.From the above, you can see the name of the event vs. what you thought it was
So, it looks like you're missing the
section
before your own section name.Second, while working on a development box, the best way to find the event you're looking for is to log things at the source. Temporarily add some debugging code to the
dispatchEvent
function.This will dump a huge list of event names out to your log. I typically use OS X's
Console.app
to view the log file during the request, copy the lines out, sort and remove duplicates, and then end up with a list like thisYou still need to use some intelligence guessing to figure out which event you want, but they're named intuitively enough that you can usually find what you're looking for.
您需要将观察者方法绑定到特定的 Magento 事件(您可以添加自己的事件,但需要找到何时触发它并添加您自己的dispatchEvent 调用)。如果 Magento 有内置事件,请在配置中使用该事件名称。
网络上有一个内置事件列表的 pdf 文件 - google for it &你会找到的。
You need to tie your observer method to a specific Magento event (you can add your own, but need to find when you want it to be fired and add your own dispatchEvent call). If Magento has an event built in, use that event name in the config.
There's a pdf of the built in event lists on the web - google for it & you'll find it.