我在哪里错误地使用了Magento';什么是观察者模式?

我在哪里错误地使用了Magento';什么是观察者模式?,magento,magento-1.5,observer-pattern,Magento,Magento 1.5,Observer Pattern,我阅读了大量关于Magento自定义模块创建的文档 在我的第一次尝试中,我使用创建了模块结构,这是我在/app/code/local/Test/MyModule/etc/config.xml中添加的代码: <?xml version="1.0"?> <config> <modules> <Test_MyModule> <version>0.1.0</version>

我阅读了大量关于Magento自定义模块创建的文档

在我的第一次尝试中,我使用创建了模块结构,这是我在
/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/code/local/Test/MyModule/Model/Observer.php

<?php
class Test_MyModule_Model_Observer
{
    public function sendOrder()
    {
        // do something.
    }
}

我可以看到两个问题,它们都是相关的。您正在使用Mage::getModel接受的语法指定要使用的类,但您的语法a.)稍有错误,b.)似乎没有实际声明模型包含的位置(除非为了更简洁而将其删除)

您需要将模型添加到全局节点中

<models>
   <testmodule>
       <class>Test_MyModule_Model</class>
   </testmodule>
<models>

测试模块
testmodule部件可以是您喜欢的任何部件,只要它对于您的模块是唯一的。观察者部分中使用的类值将变为

<class>testmodule/observer</class>
testmodule/observer

此外,我建议公共函数sendOrder()接受$observer参数,因此这可能不是严格要求的公共函数sendOrder($observer),但您是对的,它肯定会增加更多的清晰度。工作起来很有魅力@JevgeniSmirnov是的,你是对的,在实函数中,我将使用observer参数来做函数需要做的事情;我发布的函数只是一个示例;)
<class>testmodule/observer</class>