开发者

Magento Extension Override Resource File

How do you override a resource file like the following when creating a Magento extension:

core/Mage/Catalog/Model/Resource/Product/Indexer/Price/Default.php

I know that you can copy this file to the local path and override it that way, but I'm trying to figure out how to override/extend it for an extension. Specifically, what does the config.xml syntax need to look like? I'm wondering whether this is even possible, because all I see online is how to override model fil开发者_开发知识库es like this one:

core/Mage/Catalog/Model/Product.php

Which you could do with the following:

<models>
  <catalog>
    <rewrite>
      <product>My_Module_Catalog_Model_Product</product>
    </rewrite>
  </catalog>
<models>

or resource files like this one inside of the Eav/Mysql4 directory:

core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product.php

Which I believe you could do like this:

<models>
  <catalog_resource_eav_mysql4>
    <rewrite>
       <product>My_Module_Catalog_Model_Resource_Eav_Mysql4_Product</attribute></product>
  </catalog_resource_eav_mysql4>
</models>

But I don't see how to handle resource files that are not within the Eav directory. Is it possible?


From the comments on the accepted answer Zyava linked to, you can check the original Module's config.xml file to see what Models are defined. Once you know that, the syntax to override them is the same.

The Catalog Module has this Model defined for resources:

<global>
    <models>
        ....
        <catalog_resource>
            <class>Mage_Catalog_Model_Resource</class>
            ...
        </catalog_resource>
    </models>
</global>

(Note: There is no Mage_Catalog_Model_Resource class, but that's okay because Magento will use this as a base to call all related Models.)

Now we know we should use catalog_resource as the XML container for the rewrite, and we know we should use the text that comes after "resource" in the Model's class name to construct the XML container referring to the specific Model we want to override.

core/Mage/Catalog/Model/Resource/Product/Indexer/Price/Default.php
xxxxxxxxxx----------------------|-----------------------------xxxx
                              split

Catalog/Model/Resource        -> catalog_resource
Product/Indexer/Price/Default -> product_indexer_price_default    

This is how it should look in the custom Module's config.xml file:

<global>
    <models>
        <catalog_resource>
            <rewrite>
                <product_indexer_price_default>CompanyName_ModuleName_Model_Catalog_Resource_Product_Indexer_Price_Default</product_indexer_price_default>
            <rewrite>
        </catalog_resource>
    </models>
</global>

Exactly what class name you override with depends on your Module's configuration. The sample I've used (CompanyName_ModuleName_Model_Catalog_Resource_Product_Indexer_Price_Default) is the one that would fit the file structure I typically use. Yours looks like it might be something like this:

My_Module_Catalog_Model_Resource_Product_Indexer_Price_Default


What I understand now about the Magento class rewrite system, which I did not understand when I posed this question, is that this particular class that I wanted to override in an extension, is not available to be rewritten. The class in question is really more of an abstract-like class, it's never instantiated directly, instead it is subclassed by other classes which are, and which are in turn candidates for rewriting. So the correct way to achieve what I wanted would be to pick the appropriate concrete child class and rewrite it in the normal way, as outlined for instance by losttime's answer here.

And how do you determine whether a class is a candidate for rewriting? One way is after you determine the grouped class name, or URI, for the class (ie catalog/product_index_price), search for it in the Magento source and see whether it's used anywhere to actually instantiate the class.


as you can see this is also a model file under core/Mage/Catalog/Model/ dir so the extending this in config.xml (rewriting and then extending) looks the same like with any other model.

if you wan't completely override it then just copy to

local/Mage/Catalog/Model/Resource/Product/Indexer/Price/Default.php
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜