【问题标题】:magento cannot override core modelmagento 无法覆盖核心模型
【发布时间】:2012-02-16 05:14:27
【问题描述】:

首先,对于在这里提出另一个“magento core override”问题,我深表歉意,但我遵循了大约 10 个教程并阅读了此处发布的几乎所有类似问题,但没有成功。

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

我的第一次尝试是重写 Mage_Sales_Model_Order_Pdf_Invoice 类。 好的,所以我制作了我的模块。文件结构为:

app/code/local/[命名空间]/Sales/etc/config.xml

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

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

app/etc/modules/[命名空间]_Sales.xml

[namespace]_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 找到了我的自定义模型。我通过进入后端检查并查看了 System->configuration->advanced

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

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

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

【问题讨论】:

  • 为什么这里有空格? &lt; ?xml version="1.0"?&gt;
  • 我从示例中复制了 xml 结构,并将其更改为我的自定义模块。我不认为空白是必要的,但它也可以这样工作。

标签: php magento


【解决方案1】:

我从字面上发现了一些错误。请纠正这些错误:-

您在问题中提到的“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”和“Zend”)。您只需要创建这些库类的对象,就可以充分使用这些方法。

希望对你有帮助。

【讨论】:

  • 是的,我写的问题有点快而且打错了。我所有的文件当然都在 app/code/local 文件夹中,而不是 app/local。不过我会看看你的其他建议,谢谢你的帮助:)
  • 谢谢,它成功了。 config.xml 文件有错误,就像你说的那样。
猜你喜欢
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多