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.
11 comments
Can anyone please explain the diff. b/w Model and Resource Model ?
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.
I downloaded your modul’e source and you had an issue with the Collections.php file
Hopefully this will help out anybody else troubleshooting this in the future.
Very good tutorial, as usual with inchoo 🙂 🙂
@max, try to look at this tutorial : http://www.about-magento.com/magento-model-database-tutorial-54
See you,
Pierre.
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
Thanks a lot!
This article and this answer on stackoverflow:
http://stackoverflow.com/questions/4349058/magento-possible-to-have-multiple-tables-for-a-single-model
definitely helped me to understand how to setup multiple models per module 🙂
Is it possible to create a new attribute type similar to the way you created a new product type?
Hi Pablo,
Everything is possible in web development 🙂
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
@m0sh3g Yes, thanks for the heads up. I modified the post to correct it now 🙂
I think instead of
$someModel->someField(Some value);
You meant :
$someModel->setSomeField(“Some value”);