1

我知道我可以使用模块的 config.xml 中的以下语法覆盖/重写模块中的模型类:

<config>
    <global>
        <models>
            <moduletooverride>
                <rewrite>
                    <modeltooverride>Mycompany_Mymodule_Model_Myfolder_Myclass</customer>
                </rewrite>
            </moduletooverride>
         </models>
     </global>
</config>

但是,如果我希望这是有条件的(例如,基于我的模块 adminhtml 配置部分中的某些设置?)

有什么语法吗?

或者,有没有办法让我重写的班级像我重写之前一样进入班级?(允许它称其为“前身”。

4

3 回答 3

5

没有内置的配置选项可以让您有条件地重写这样的类。

但是,重写的类只是一个扩展类,因此所有标准 OOP 规则都适用,包括使用parent:: So 之类的

class My_Rewritten_Class extends Class_I_Rewrote
{

    public function theMethodIRewrote($param, $options)
    {
        $original_results = parent::theMethodIRewrote($param, $options);
        if(!Mage::getStoreConfigFlag('path/to_my/on_or_off_flag'))
        {
            return $original_results
        }

        //continue with the rewrite
    }
}

最后,虽然我从未尝试过,但您应该能够获得对已解析配置选项的引用

$config->Mage::getConfig();

setOptions然后使用其方法手动设置或取消设置您的重写选项。

于 2011-01-18T18:03:19.380 回答
2

在这个讨论中有几个很好的建议:通过商店或网站限制模块功能的最佳方法是什么

特别是,这个答案有一个简短而甜蜜的技术,不仅可以用于存储条件。

于 2011-01-18T13:20:16.590 回答
1

类似的方法 parent::theMethodIRewrote($param, $options) 并不总是可用的,因此请尝试以下非常简单的解决方案:

if (!Mage::helper('mymodule')->isEnabled()){

    class My_Rewritten_Class extends Class_I_Rewrote{} //empty body - nothing rewritten

}else{

    class My_Rewritten_Class extends Class_I_Rewrote{

        public function theMethodIRewrote($param, $options){
              /* method body ... */
        }

        /* other methods ... */

    }

}

在我的情况下(magento 1.6.0.0,php 5.3)它似乎正在工作。

于 2012-06-01T09:47:24.340 回答