我的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()方法永远不会被调用。 注意:产品和目录的其他方法工作正常。
任何帮助解决此问题或任何其他方法都将受到赞赏
答案 0 :(得分:8)
Mage_Cms_Model_Block
不会覆盖_eventPrefix
属性,因此在保存之前它只会触发两个通用事件:model_save_before
和core_abstract_save_before
。
虽然您已经在重写CMS块模型,但您可以简单地覆盖_eventPrefix
属性并将其设置为cms_block
;这将允许你的观察者工作。但是,因为您已经使用模型作为观察者执行重写和,所以您可以简单地覆盖_beforeSave()
方法并在那里添加您的逻辑并简单地添加一些东西。
修改:您的重写无效。重写cms/block
模型的语法是
<global>
<models>
<cms>
<rewrite>
<block>Alchemy_Catalog_Model_Block</block>
</rewrite>
</cms>
</models>
</global>