Getting started with Magento ORM, setting up the model

Getting started with Magento ORM, setting up the model

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
$someModel->save();

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');
$model->setField1('Branko');
$model->setField2('Ajzele');
$model->save();

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.

Cheers.

You made it all the way down here so you must have enjoyed this post! You may also like:

How to manage clients, pressure and stress Zelimir Gusak
Zelimir Gusak, | 3

How to manage clients, pressure and stress

Extending “Contact us” functionality in Magento Ivan Bernatovic
Ivan Bernatovic, | 8

Extending “Contact us” functionality in Magento

Google Shopping campaign – how to make a detailed product level performance report Tea Pisac Benes
Tea Pisac Benes, | 3

Google Shopping campaign – how to make a detailed product level performance report

11 comments

    1. A “resource model” is a class that, behind the scenes, does the actual fetching of data from Magento. Every model has a resource model that is used to load a single instance of a model from the database. A “collection” is a class that loads an array like structure of multiple models based on a set of rules.

      or You can visit the page Model vs Resource model.

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

    public function __construct()
        {
    
    		$this->_init('hello/hello');
        }
    Should remove the extra underscore in the construct function to get the native getCollection() working.
    
    public function _construct()
        {
    
    		$this->_init('hello/hello');
        }

    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. 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.
    thanks

  4. 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>.

Tell us about your project

Drop us a line. We'd love to know more about your project.