magento无法覆盖核心模型 [英] magento cannot override core model

查看:75
本文介绍了magento无法覆盖核心模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我很抱歉在这里提出另一个"magento核心替代"问题,但是我遵循了大约10个教程,并通读了这里发布的几乎所有类似问题,但没有成功.

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

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

应用程序/代码/本地/[名称空间]/Sales/etc/config.xml

应用/代码/本地/[命名空间]/Sales/Helper/Data.php (该类不执行任何操作,它只是一个空类.之所以这样做,是因为我读到某个地方,如果没有Helper类,Magento有时无法识别该模块)

应用程序/代码/本地/[名称空间]/销售/模型/订单/Pdf/Invoice.php

app/etc/modules/[namespace] _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

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

解决方案

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

您在问题中在"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").您只需要创建这些库类的对象,就可以完全使用这些方法.

希望有帮助.

first of all, I apologize for asking yet another "magento core override" question here, but I followed about 10 tutorials and read through almost all similar questions posted here, no success.

I have to override a bunch of core models and classes. The code works, because I already changed the core (in a test magento site) and it worked perfect. But every now and then a magento update is available and if we would apply the updates all of my changes would be lost. So I have to override the base code instead. I want to make my own module to put in all of the required code, because I only have to override 1 or 2 functions in every class, the rest should work like Magento intended.

My first attempt was to override the Mage_Sales_Model_Order_Pdf_Invoice class. Ok, so I made my module. The file structure is:

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

app/code/local/[namespace]/Sales/Helper/Data.php (This class doesn't do anything, it's just an empty class. I made it because I read somewhere that Magento sometimes doesn't recognize the module if there is no Helper class)

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

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

The [namespace]_Sales.xml file looks this way:

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

The config.xml file looks like this:

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

And the Invoice.php file looks like this:

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


}

I wanted to test this first before I go and override all the other controllers and models I have to change.

The problem is, it still uses the original model.

I think the module code and paths are correct, because magento finds my custom model. I checked by going into the backend and looked at System->configuration->advanced

I cleared the cache completely, so that's not it.

I used get_class to determine what model is returned in the controller: get_class(Mage::getModel('sales/order_pdf_invoice')), this returns Mage_Sales_Model_Order_Pdf_Invoice

I do not know where I made a mistake but I am sure I made one :(

解决方案

There are some mistakes which I have found literally. Please correct those mistakes:-

All the file structures, which you have mentioned in the question within the "local" code pool, have a missing folder name "code" inside the "app" folder. So every file structure of your local module must be like: "app/code/local/[namespace]/Sales/...".

If this folder structure is wrong, then also your [namespace]_Sales module may not work as expected.

Secondly, The contents of the file "config.xml" is a bit wrong. The correct one will be:-

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

Also I don't think you will require adding different classes here (which you have done in "[namespace]_Sales_Model_Order_Pdf_Invoice" class PHP page). This is because Magento loads all the definitions of related libraries automatically (some examples of library classes are of "Varien" and "Zend"). You will just need to make an object of those library classes and you will be able to use the methods fully.

Hope it helps.

这篇关于magento无法覆盖核心模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆