7

我的 Alchemy 目录模块中有这个 XML 结构:

<?xml version="1.0" encoding="UTF-8"?>
<!--
    @filepath /app/code/local/Alchemy/Catalog/etc
The XML has been extended following
Magento Events API Observers
http://www.excellencemagentoblog.com/magento-part11-series-eventsapi
or
http://blog.chapagain.com.np/magento-event-observer-with-save-before-and-save-after/
-->
<config>
    <modules>
        <Alchemy_Catalog>
            <version>0.1.0</version>
        </Alchemy_Catalog>
    </modules>

    <global>
        <models>
            <alchemycatalog>
                <rewrite>
                    <product>Alchemy_Catalog_Model_Product</product>
                    <block>Alchemy_Catalog_Model_Block</block>
                </rewrite>
            </alchemycatalog>
        </models>
        <events>
            <!--
            Examples: catalog_product_save_before, catalog_product_prepare_save
            Check out Magento events cheat sheet at http://www.nicksays.co.uk/magento-events-cheat-sheet-1-7/ -->
            <catalog_product_save_after>
                <observers>
                    <Alchemy_Catalog>
                        <type>singleton</type>
                        <class>Alchemy_Catalog_Model_Product</class>
                        <method>pingBaseProductService</method>
                    </Alchemy_Catalog>
                </observers>
            </catalog_product_save_after>
            <catalog_category_save_after>
                <observers>
                    <Alchemy_Catalog>
                        <type>singleton</type>
                        <class>Alchemy_Catalog_Model_Product</class>
                        <method>pingBaseCategoryService</method>
                    </Alchemy_Catalog>
                </observers>
            </catalog_category_save_after>
            <cms_block_save_before>
                <observers>
                    <Alchemy_Catalog>
                        <type>singleton</type>
                        <class>Alchemy_Catalog_Model_Block</class>
                        <method>rabbitmqBlockProducer</method>
                    </Alchemy_Catalog>
                </observers>
            </cms_block_save_before>
        </events>
    </global>
</config>

这个模型应该记录一条消息来证明它有效:

<?php
class Alchemy_Catalog_Model_Block extends Mage_Cms_Model_Block {
    /**
     * Implement function rabbitmqBlockProducer().
     * This function writes a message to the rabbit
     * mq server
     */
    protected $_eventPrefix = 'cms_block';

    public function rabbitmqBlockProducer ($event) {
        Mage::log('save3 block invoked', null, 'marian.log');
    }
    /**
     * Prevent blocks recursion
     *
     * @throws Mage_Core_Exception
     * @return Mage_Core_Model_Abstract
     */
    protected function _beforeSave() {
        Mage::log('save2 block invoked', null, 'marian.log');
    }
}

但是rabbitmqBlockProducer()方法永远不会被调用。注意:产品和目录的其他方法工作得很好。

任何解决此问题或任何其他方法的帮助将不胜感激

4

1 回答 1

8

Mage_Cms_Model_Block不会覆盖该_eventPrefix属性,因此它只会在保存之前触发两个通用事件:model_save_beforecore_abstract_save_before.

虽然您已经在重写 CMS 块模型,但您可以简单地覆盖该_eventPrefix属性并将其设置为cms_block; 这将允许您的观察者工作。但是,因为您已经在执行重写并将模型用作观察者,所以您可以简单地覆盖该_beforeSave()方法并在其中添加您的逻辑,然后简单地做一些事情。

编辑:您的重写不起作用。模型的重写语法cms/block

<global>
    <models>
        <cms>
            <rewrite>
                <block>Alchemy_Catalog_Model_Block</block>
            </rewrite>
        </cms>
    </models>
</global>
于 2013-07-24T12:15:23.430 回答