Magento getModel with or without parameters

Posted by Damodar Bashyal on April 30, 2013

 

In Magento you can load your model with getModel. You can optionally pass parameters to the model class constructor. All you need to do is:

Mage::getModel('model_class', 'arguments_as_array_if_any');

For example: I want to load Data.php model from: https://github.com/dbashyal/Magento-General-Module-Extension

So, to load this model class i need to call as:

Mage::getModel('tgeneral');

As Data.php is default file name, you don't need to pass file name. But if you need to load something like Observer.php from model then you have to call it as:

Mage::getModel('tgeneral/observer');

If you have a construct function and want to pass arguments then you need to load model as:

Mage::getModel('tgeneral', array('key1' => 'value1','key2' => 'value2',...));

Src: Mage.php 'getModel()' and app\code\core\Mage\Core\Model\Config.php 'getModelInstance()'

 
not published on website


QR Code: Magento getModel with or without parameters