我阅读了很多有关 Magento 自定义模块创建的文档。
对于我的第一次尝试,我使用 Module Creator 创建了模块结构。 ,这是我在 /app/code/local/Test/MyModule/etc/config.xml
中添加的代码:
<?xml version="1.0"?>
<config>
<modules>
<Test_MyModule>
<version>0.1.0</version>
</Test_MyModule>
</modules>
<!-- frontend, admin, adminhtml -->
<global>
<!-- models, resources, blocks, helpers -->
<events>
<sales_order_place_before> <!-- event i need to catch -->
<observers>
<trigger_mymodule_placeorder>
<type>model</type>
<class>test/mymodule/model_observer</class>
<method>sendOrder</method>
</trigger_mymodule_placeorder>
</observers>
</sales_order_place_before>
</events>
</global>
</config>
我的/app/etc/modules/Test_MyModule.xml
文件:
<?xml version="1.0"?>
<config>
<modules>
<Test_MyModule>
<active>true</active>
<codePool>local</codePool>
</Test_MyModule>
</modules>
</config>
这是我的/app/code/local/Test/MyModule/Model/Observer.php
:
<?php
class Test_MyModule_Model_Observer
{
public function sendOrder()
{
// do something.
}
}
..但是 Test_MyModule_Model_Observer::sendOrder() 函数永远不会被触发(我尝试在其中放入一个虚拟数据库记录器以查看该函数是否/何时执行)。
我知道模块本身已正确加载,因为在模块的config.xml中,它在主菜单中声明了一个新链接,并且该链接正确显示(刷新magento的缓存后),所以我猜问题是函数命名我在某个地方缺少的约定..但在哪里?
最佳答案
我发现您有两个相关的问题。您正在使用 Mage::getModel 接受的语法指定要使用的类,但您 a.) 语法略有错误,b.) 似乎没有实际声明模型的包含位置(除非您将其取出以便更简洁)。
您需要将模型添加到全局节点中。
<models>
<testmodule>
<class>Test_MyModule_Model</class>
</testmodule>
<models>
testmodule 部分可以是您喜欢的任何内容,只要它对于您的模块是唯一的。观察者部分使用的类值将变成...
<class>testmodule/observer</class>
关于magento - 我在哪里错误地使用了 Magento 的观察者模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9017617/