Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Extending a Model’s Code
 
ShakespeareGeek
Jr. Member
 
Total Posts:  22
Joined:  2009-05-05
 

I was going to ask about actually modifying the database and adding attributes, but have decided that I need to start small.

Let’s say that I want to modify the code for a particular Model.  I want to add a method.  That is all.

*Where* do I do that?  Does Magento have a Ruby/Smalltalk-like way of saying “I am redefining Order object so that it also has this method”? 

Or is it more like Java where you’d have to create a whole class called MyOrder which extends Order, and then put your method on MyOrder?  And if I have to do that—where do I put the MyOrder object definition, and how many different ways will it ripple the system that I have to find and change?  Would I have to go everywhere that somebody instantiated an Order class and replace it with MyOrder?

If the answer to the question is “Just modify the definition of Order.php directly in the core source tree” then my next question is, “Is that a good practice?  what happens when the next version of Magento comes out?” I am trying to play nicely now and do the “right thing” so I don’t have upgrade issues later.

What I ultimately want to do is in fact add fields to the database definition of Order and then extend it all the way through to the admin interface (has to do with our credentials generator).  I’m just trying to break it down into pieces that I can understand.  I’m new at Magento (can you tell?) and even PHP is not really my game, I’ve got more experience with Java/Ruby/C#.  So if I’m doomed, be gentle.

 
Magento Community Magento Community
Magento Community
Magento Community
 
ws1984
Jr. Member
 
Total Posts:  8
Joined:  2009-03-04
 

hi and welcome :D

the best way to do this is create your own local module and extend appropriate model : wiki page on customization

 
Magento Community Magento Community
Magento Community
Magento Community
 
ShakespeareGeek
Jr. Member
 
Total Posts:  22
Joined:  2009-05-05
 
ws1984 - 12 June 2009 10:29 AM

hi and welcome :D

the best way to do this is create your own local module and extend appropriate model : wiki page on customization

Looks like just what I need to get started.  Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
ShakespeareGeek
Jr. Member
 
Total Posts:  22
Joined:  2009-05-05
 
ShakespeareGeek - 12 June 2009 10:34 AM

ws1984 - 12 June 2009 10:29 AM
hi and welcome :D

the best way to do this is create your own local module and extend appropriate model : wiki page on customization

Looks like just what I need to get started.  Thanks!

Yeah, I’m still confused :(.  Is it me or at times does that page use “Model” and “Module” interchangeably?  ”Models are classes that define abstract or resource specific logic. Here’s how we are going to declare a new module functionality” So later when it abbreviates things to “mod” (like ‘test_mod’ and ‘example_mod’) I have no idea whether that means a module or a model, and what I’m supposed to swap in for my own code.

I get lost in Example_This and Example_That.  I want to extend Mage_Sales_Model_Order with a new method.  I create a new module called MyCompany_Core, I think I did that right.  So am I now expected to mimic the tree and have MyCompany_Core_Sales_Model_Order or is that up to me?  I think I can get my actual class defined, it’s the bit where I say “And I’d like this to take the place of Mage_Sales_Model_Order please” that I’m still stuck.

Thanks for the help!

 
Magento Community Magento Community
Magento Community
Magento Community
 
ws1984
Jr. Member
 
Total Posts:  8
Joined:  2009-03-04
 

I try to write a little howto but not tested, not during week end wink

1 : create directories /app/code/local/MyCompany/MyModule

2 : then create your config.xml in /app/code/local/MyCompany/MyModule/etc/ :

<?xml version="1.0" encoding="utf-8"?>
<config>
  <
modules>
    <
MyCompany_MyModule>
      <
version>0.0.1</version>
    </
MyCompany_MyModule>
  </
modules>
  <global>
    <
models>
      <
sales>
        <
rewrite>
          <
order>MyCompany_MyModule_Model_Order</order>
        </
rewrite>
      </
sales>
    </
models>
  </global>
</
config>

3 : create your own model in /app/code/local/MyCompany/MyModule/Model/Order.php :

// extends model you want to customize
class MyCompany_MyModule_Model_Order extends  Mage_Sales_Model_Order
{
// your own code

4 : load your module by creating file MyCompany_MyModule.xml in /app/etc/Modules :

<?xml version="1.0"?>
<config>
  <
modules>
    <
MyCompany_MyModule>
      <
active>true</active>
      <
codePool>local</codePool>
    </
MyCompany_MyModule>
  </
modules>
</
config>

5 : check in back office your module is enable

i hope it works, but it’s the good way !

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top