Magento 扩展覆盖资源文件

发布于 2024-12-06 09:55:36 字数 975 浏览 0 评论 0原文

创建 Magento 扩展时如何覆盖如下资源文件:

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

我知道您可以将此文件复制到本地路径并以这种方式覆盖它,但我试图弄清楚如何覆盖/扩展它扩大。具体来说,config.xml 语法需要是什么样子?我想知道这是否可能,因为我在网上看到的只是如何覆盖像这样的模型文件:

core/Mage/Catalog/Model/Product.php

您可以使用以下内容来执行此操作:

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

或 Eav/Mysql4 目录内的像这样的资源文件:

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

我相信你可以这样做:

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

但我不知道如何处理不在 Eav 目录中的资源文件。是否可以?

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 files 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

活雷疯 2024-12-13 09:55:36

从 Zyava 链接到的已接受答案的评论中,您可以检查原始模块的 config.xml 文件以查看定义了哪些模型。一旦您知道了这一点,覆盖它们的语法就是相同的。

目录模块为资源定义了这个模型:(

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

注意:没有 Mage_Catalog_Model_Resource 类,但这没关系,因为 Magento 将使用它作为调用所有相关模型的基础。)

现在我们知道我们应该使用catalog_resource 作为重写的 XML 容器,我们知道应该使用 Model 类名中“resource”后面的文本来构造引用特定 Model 的 XML 容器我们想要覆盖。

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    

这就是它在自定义模块的 config.xml 文件中的外观:

<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>

您所覆盖的确切类名取决于您的模块的配置。我使用的示例 (CompanyName_ModuleName_Model_Catalog_Resource_Product_Indexer_Price_Default) 适合我通常使用的文件结构。你的看起来可能是这样的:

My_Module_Catalog_Model_Resource_Product_Indexer_Price_Default

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
花开柳相依 2024-12-13 09:55:36

我现在对 Magento 类重写系统的理解(当我提出这个问题时我并不理解)是,我想在扩展中重写的这个特定类无法被重写。所讨论的类实际上更像是一个类似抽象的类,它从不直接实例化,而是由其他类创建子类,而这些类又是重写的候选类。因此,实现我想要的正确方法是选择适当的具体子类并以正常方式重写它,例如此处losttime的答案所概述的那样。

如何确定一个类是否适合重写?一种方法是在确定该类的分组类名或 URI(即catalog/product_index_price)后,在 Magento 源代码中搜索它并查看它是否在任何地方用于实际实例化该类。

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.

陌路终见情 2024-12-13 09:55:36

正如您所看到的,这也是 core/Mage/Catalog/Model/ 目录下的模型文件,因此在 config.xml 中扩展它(重写然后扩展)看起来与任何其他模型相同。

如果你不想完全覆盖它,那么只需复制到

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文