magento无法覆盖核心模型


magento cannot override core model

首先,我很抱歉在这里问了另一个"magento core override"问题,但我遵循了大约10个教程,阅读了这里发布的几乎所有类似问题,但没有成功。

我必须覆盖一堆核心模型和类。代码是有效的,因为我已经更改了核心(在一个测试magento站点中),它运行得很完美。但每隔一段时间就会有一个magento更新可用,如果我们应用更新,我的所有更改都会丢失。所以我必须重写基本代码。我想制作我自己的模块来放入所有需要的代码,因为我只需要覆盖每个类中的1或2个函数,其余的应该像Magento想要的那样工作。

我的第一次尝试是覆盖Mage_Sales_Model_Order_Pdf_Invoice类。好的,所以我做了我的模块。文件结构为:

app/code/local/[namespace]/Sales/etc/config.xml

app/code/local/[namespace]/Sales/Helper/Data.php(这个类什么都不做,它只是一个空类。我之所以创建它,是因为我在某个地方读到,如果没有Helper类,Magento有时无法识别模块)

app/code/local/[namespace]/Sales/Model/Order/Pdf/Invoice.php

app/etc/modules/[namespace]_Sales.xml

[名称空间]_Sales.xml文件如下所示:

<?xml version="1.0"?>
    <config>
        <modules>
            <[namespace]_Sales>
                <active>true</active>
                <codePool>local</codePool>
            </[namespace]_Sales>
        </modules>
    </config>

config.xml文件如下所示:

< ?xml version="1.0"?>
  <config>
    <modules>
        <[namespace]_Sales>
            <version>0.1.0</version>
        </[namespace]_Sales>
    </modules>
    <global>
    <helpers>
            <sales>
                <class>[namespace]_Sales_Helper</class>
            </sales>
        </helpers>
       <models>
          <sales>
              <rewrite>
                  <order_pdf_invoice>[namespace]_Sales_Model_Order_Pdf_Invoice</order_pdf_invoice>
              </rewrite>
          </sales>
       </models>
    </global>
</config>

Invoice.php文件如下所示:

<?php
 /****I'm adding some different classes here*******************************/
 include_once Mage::getBaseDir('lib')."/myclass.php";
 include_once Mage::getBaseDir('lib')."/another_library.php";
 /********************************************************/
class [namespace]_Sales_Model_Order_Pdf_Invoice extends Mage_Sales_Model_Order_Pdf_Invoice
{
    public function getPdf($invoices = array())
    {
         //my code
    }

}

我想先测试一下,然后再去覆盖我必须更改的所有其他控制器和模型。

问题是,它仍然使用原始模型。

我认为模块代码和路径是正确的,因为magento找到了我的自定义模型。我进入后端进行了检查,查看了系统->配置->高级

我完全清除了缓存,所以不是这样。

我使用get_class来确定控制器中返回的模型:get_class(Mage::getModel('sales/order_pdf_invoice')),返回Mage_sales_model_order_pdf_invoice

我不知道我在哪里犯了错误,但我确信我犯了一个错误:(

我确实发现了一些错误。请纠正这些错误:-

您在问题中提到的"local"代码池中的所有文件结构,在"app"文件夹中都缺少文件夹名"code"。因此,本地模块的每个文件结构都必须类似于:"app/code/local/[namespace]/Sales/..."

如果此文件夹结构错误,则[namespace]_Sales模块也可能无法按预期工作。

其次,文件"config.xml"的内容有点错误。正确的是:-

<?xml version="1.0"?>
<config>
  <modules>
    <[namespace]_Sales>
      <version>0.1.0</version>
    </[namespace]_Sales>
  </modules>
  <global>
    <helpers>
      <!--
      This node will be the unique identifier of your module,
      and it will be used every time your code requires referencing your own module.
      This shouldn't clash with other unique identifiers used in your Magento system.
      Normally all the characters are kept in small case for this,
      however, I haven't tried with the upper case.
      But it will be best to keep your unique identifier in small case only.
      -->
      <[namespace]sales>
        <class>[namespace]_Sales_Helper</class>
      </[namespace]sales>
    </helpers>
    <models>
      <!--
      If this is not provided, then Magento will not know your module's starting part of Model Class Names.
      -->
      <[namespace]sales>
        <class>[namespace]_Sales_Model</class>
      </[namespace]sales>
      <sales>
        <rewrite>
          <order_pdf_invoice>[namespace]_Sales_Model_Order_Pdf_Invoice</order_pdf_invoice>
        </rewrite>
      </sales>
    </models>
  </global>
</config>

此外,我认为您不需要在这里添加不同的类(您已经在"[namespace]_Sales_Model_Order_Pdf_Invoice"类PHP页面中完成了这一操作)。这是因为Magento自动加载所有相关库的定义(库类的一些例子是"Varien"answers"Zend")。您只需要创建这些库类的对象,就可以完全使用这些方法。

希望能有所帮助。