Magento模型重写的最佳实践是什么?(What is the best practices in Magento model rewrite?)

编程入门 行业动态 更新时间:2024-10-22 23:13:41
Magento模型重写的最佳实践是什么?(What is the best practices in Magento model rewrite?)

我公司正在为Magento制作一个模块,以满足巴西商业规则的需求。

有时我们需要覆盖核心模型来添加功能,有时需要重写核心模型来改变一些功能。 我们怀疑这是做这种变化的最佳做法。 例如,当模块需要修改其他模型所拥有的某些行为时。

我们知道修改后的文件需要在“local / mycompany / modules ...”下创建,但是我们遇到了一个问题,我将在下面介绍。

一个简单的例子是:我们的客户模型需要添加带有'99999-999'掩码的邮政编码,在最后三个字符之前添加' - '。 没关系,它的完成和工作。 但是当我们需要使用这个邮政编码进行计算时,我们需要删除' - '字符。 这是在我们重写的“Shipping / Rate / Request.php”模型中做出的。

这是我们怀疑的点。 哪个最好?

这是在“local / mycompany / Shipping / Model / Rate / Request.php”中的“Shipping”模块内重写还是在“Customer”模块目录结构中重写?

我们对我们需要决定的结构感到困惑。

重要的是我们不会分发或出售重新分类为模块的模块。

我会在这里放一些代码:

邮政编码由我们的“客户”模块接收,输入掩码,由Javascript完成。 此“客户”模块向客户数据库添加更多字段。

在这里,我在“Customer”模块中重写了“Shipping / Rate / Request.php”

在“客户”模块的config.xml中:

<shipping> <rewrite> <rate_request>Mycompany_Customer_Model_Shipping_Rate_Request</rate_request> </rewrite> </shipping>

然后文件“Mycompany / Customer / Model / Shipping / Rate / Request.php”包含从邮政编码中删除“ - ”字符的代码。

我们的疑问是:在某些情况下,我们需要再次重写“Shipping / Rate / Request.php”来修改其他方法。 哪个最好?

每次重写都是在来自哪个模块内完成的? 创建相同类的重复文件。 在“Mycompany”命名空间内使用相同的Magento核心目录结构,并在每个类的一个文件中修改我们需要的所有内容? 像Magento的核心?

我不知道我是否可以解释我需要什么。

谢谢!

My company is making a module for Magento to fit the needs of Brazilian business rules.

Sometimes we need to override core models to add funcionality, sometimes need to rewrite core models to change some functionality. We are in doubt wich is the best practices to do this changes. For example, when a module needs to modify some behavior that is owned by other model.

We know that the modified files need to be created under "local/mycompany/modules..." but we're running into a problem that I will describe below.

A simple example is: Our Customer model needs to add the postcode with the '99999-999' mask, with the '-' before the last three chars. That is ok, its done and working. But when we need to use this postcode to make calculations, wee need to remove the '-' char. That was made in our rewrited "Shipping/Rate/Request.php" model.

That is the point of our doubt. Which is best?

Do this rewrite inside new "Shipping" module in "local/mycompany/Shipping/Model/Rate/Request.php" or do this rewrite inside the "Customer" module directory structure?

We are confusing about the structure we need to decide.

Something important is that we won't distribute or sell the modules rewrited as separated modules.

I'll put some code here:

The postcode is received by our "Customer" module, with masks in input, done by Javascript. This "Customer" module add more fields to customer database.

Here, I rewrite "Shipping/Rate/Request.php", inside the "Customer" module:

in config.xml of "Customers" module:

<shipping> <rewrite> <rate_request>Mycompany_Customer_Model_Shipping_Rate_Request</rate_request> </rewrite> </shipping>

Then the file "Mycompany/Customer/Model/Shipping/Rate/Request.php" contains the code to remove '-' char from postcode.

Our doubt is: In some cases, we need to rewrite the "Shipping/Rate/Request.php" again to modify other methods. Which is best?

Every rewrite is done inside the module that came from? Creating repeated files of the same class. Use the same directory structure of Magento's core inside the "Mycompany" namespace and modify all that we need inside one file of each class? Like Magento's core?

I don't know if I could explain what I need.

Thanks!

最满意答案

最好将模块的内容保存在其模块文件夹中。

如果您正在谈论的扩展是模块功能的一部分,那么您应该将它们放在该模块的文件夹中。 然后路径将是local / mycompany / Mymodule / Model / Shipping / Rate / Request.php。

我会在dir结构中创建这个额外的级别,以便更清楚重写的Request.php来自Shipping模块,但这绝对不是必需的。

这是一个很好的做法当然是模块化的原因。 您将能够更好地添加,删除和版本一个文件夹中包含的模块。 想想app / etc / modules中的模块xml文件和模块目录中的配置文件。 它们描述了模块目录中的内容,而不是所有属于它的重写但位于不同模块文件夹中的重写。

此外,如果您从多个模块扩展或重写了相同的模型,并且您决定删除或完全重写您自己的模块,如果它们分散在应用程序目录周围,您将如何知道哪些文件或文件部分属于它? 。

有关如何创建Magento模块的更详细视图,请查看http://www.magentocommerce.com/magento-connect/create_your_extension/

It's best to keep the contents of a module within it's module folder.

If the extends you are talking about are part of a module's functionality than you should place them inside that module's folder. The path would then be local/mycompany/Mymodule/Model/Shipping/Rate/Request.php.

I would create this extra level in the dir structure to make it clearer that the rewritten Request.php comes form the Shipping module, but this is absolutely not a necessity.

The reason why this is a good practice is of course modularity. You will be much more able to add, remove and version modules that are contained within one folder. Think about the module's xml file in app/etc/modules and the config files in your module's directory. They describe what is in your module's directory and not all the rewrites that belong to it but are in different module folders.

Besides, if you have extended or rewritten the same model from multiple modules and you decide to remove or totally rewrite a module of your own, how will you know which files or parts of files will belong to it if they are scattered around the app dir.

For a more detailed view on how to create Magento modules please also have a look at http://www.magentocommerce.com/magento-connect/create_your_extension/

更多推荐

本文发布于:2023-08-01 03:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353859.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   模型   Magento   rewrite   model

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!