1
votes

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?

  1. Every rewrite is done inside the module that came from? Creating repeated files of the same class.
  2. 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!

1
Please add some code examples and split up your text, to make it more readable. That way will you get more answers.jokklan

1 Answers

0
votes

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/