Getting started with Magento ORM, setting up the model

Featured Image

Given the sheer size of Magento system, its quite easy to loose valuable time when trying to achieve even the basic things like “Creating simplest ORM model class”. Substantial amount of serious programming in Magento requires you to write some values to database.

By now, most of us have seen how “cool” Magento models are, and how “easy” it is to write values to models by using syntax like

//$someModel = new Company_Modul_Model_SomeModel();
//or the preferable way
$someModel = Magent::getModel('company/somemodel');
/* To use some existing model record from database */
//$someModel->load(); /* Some valid id in database */
//$someModel->setSomeField = 'Some value'; 
//or the preferable way
$someModel->setSomeField('Some value');
//Save model

Code example shown above is clean, easy to use, etc. Probably the main thing to note is the usage of “$someModel->someField = ‘Some value’;” or “$someModel->someField(Some value);”. Safest, and more “cooler” way would be to assign field values trough “virtual methods” like “$someModel->someField(Some value);”. By doing so you are preparing a “safe ground” for yourself for possible latter modifications on individual fields (attributes) of model before “save” operation.

Lets say you wish to parse some logic on certain field before its set, in that case its easier to do “$someModel->someField(Some value);” and later implement the actual “someField()” method inside the model that handles the necessary logic.

Hopefully you catch my point 🙂

Moving along, the model itself. To get the model class to behave like the one shown above, you need to implement it in the proper way.

Attached is a sample module, that does exactly that. All you need to do is un-archive it, copy-paste the /app folder to your demo store, and refresh the demo store home page.

Module creates database table called “ajzele_hello” and appropriate “installation mark” entry in core_resource table called “hello_setup“.

Once you refreshed your home page, you should be able to do something like:

//$model = new Ajzele_SimpleModel_Model_Hello();
$model = Mage::getModel('hello/hello');

This should make a new entry in the database table “ajzele_hello”.

Download Ajzele_SimpleModel.

Note, DO NOT USE this module on live site. This is merely for educational purposes. As much as I can confirm that module is essentially simple and it works out of the box, I do not wish to comment on any “it broke my site” replies.



  1. I downloaded your modul’e source and you had an issue with the Collections.php file

    public function __construct()
    Should remove the extra underscore in the construct function to get the native getCollection() working.
    public function _construct()

    Hopefully this will help out anybody else troubleshooting this in the future.

  2. Hi I tried your module it works fine, and i found it helpful. But I have a problem: if alter table by adding a column i can’t set records of my new column.
    But if i add column before the installation it works!
    I think problem about permission in table?
    Thx a lot Max

  3. Is it possible to create a new attribute type similar to the way you created a new product type?

  4. i put the files in desired folder but i dont know which page to run and how can the code create table in db itself, as i am new in magento so i need some more info about this post can u reply this post.

  5. I think instead of

    $someModel->someField(Some value);

    You meant :

    $someModel->setSomeField(“Some value”);

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.