Take the 2-minute tour ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

I want to know when we should use model rewrites ? What's the down-sides of it ?

I mean when we rewrite a core model class, we should know that no extension can rewrite the core class from now on, so we should rewrite only once or we need to write a local extension to solve rewrite conflicts which is really annoying.

So What's the alternative way ?

i.e In my new module, I want to add a very simple function to Mage_Catalog_Model_Category, so how can I do that without rewriting it ? What do you suggest other than rewriting ? Is rewriting Evil ?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

It's certainly not evil - though it does have drawbacks - it is one of those features of Magento that sets it apart and make it a flexible platform. However, like most things, too much of a good thing can quickly become very, very problematic. There are three ways of overriding core functionality:

Observers

An observer is always preferable. Observers don't have this limit of one-per-class. Observers can be slotted before or after desired controller actions. You can also tap into model load. They're pretty powerful.

However, they're not a one-size-fits-all solution and it's possible for you to not be able to pinpoint functionality.

Rewrites

Only one rewrite can exist at any time for a given class. Classes attempting to rewrite the same class over and over will have rewrite conflicts. Before you rewrite you want to determine:

  • Is this a community module that will be used by n stores with n plugins installed?
  • If so, are there common plugins which rewrite the same class you need to rewrite that perhaps you can code around?
  • If not, are you sure that this is insular enough to not cause an issue?

If the above answers can be satisfied, use a rewrite.

Local Module Overrides

This is never preferable. This is stopgap only if you have a patch for a core issue which has not been issued by Magento yet.

A word on helpers

You are able to get some code reuse by creating a custom helper. I would avoid helpers that work across modules or in view files - if the helper doesn't exist and your view depends on it then it will throw an exception. I would prefer the rewrite first; however, if this is something that is reusable in potentially dozens of places you would then use a helper. A helper essentially used to keep your code DRY.


Which one you use is up to you. The order of precedence is as defined above in my opinion. Best of luck.

share|improve this answer
    
Wow, very great explanation. So many thanks. You clarified it very carefully. Just one question, what do you suggest if I want to add a function to category class ? consider a function to show a magick number related to a specific category. What kind of solutions do you suggest in this situation ? –  Miko Jul 16 at 15:25
1  
That will be a rewrite to add functionality. Great use case for when an observer would not be preferable. –  philwinkle Jul 16 at 15:26
    
Great...Thanks for the answer. Isn't a helper do the trick for a small feature like mine I just explained ? I mean create a helper function in my module that anyone can pass category id and then get magick number ? Is it better/safer solution than rewrite? –  Miko Jul 16 at 15:28
1  
I would avoid helpers that work across modules or in view files - if the helper doesn't exist and your view depends on it then it will throw an exception. I would prefer the rewrite first; however, if this is something that is reusable in potentially dozens of places you would then use a helper. A helper essentially used to keep your code DRY –  philwinkle Jul 16 at 15:31
1  
I just added it. See my edit. Thanks! –  philwinkle Jul 16 at 20:58

Rewriting itself is not evil, it's just something you do. Also it's possible to have more than one rewrite per object, it means that you must chain them: Object B extends Object A and Object C extends Object B.

However, you could also check observers (observers are part of Event Driven Architecture). Events are basically hooks that observer is monitoring and once event is dispatched all observers associated with it get executed, no need to chain.

share|improve this answer
    
Thanks for the great answer. As I mentioned, I know that they can extends from each other but it then requires more work. For small functionality like the one I wrote as an example what do you suggest ? Can you please let me know what's the alternative for rewrites ? I mean I want to write a module that whoever installs it , he/she can execute a function to show a magick number related to a category. Should we use helpers instead ? Or something else ? So many thanks. –  Miko Jul 16 at 15:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.