Difference between getsingleton() , getmodel() and getResourceModel() In Magento.

Mage::getSingleton()

Mage::getSingleton() will first check if the same class instance exists or not in the memory. If the instance exists then it will return the same object from the memory otherwise it load the model, save into Mage Register and return it. So Mage::getSingleton() is faster than Mage::getModel().

Example:

$product1 and $product2 both will share same memory of OS and return only one instance each time.

Now take a look at the getSingleton Method in Mage.php :

Mage::getModel()

Mage::getModel() will create a new instance of an object each time even such object exists in configuration.

Example

$product1 and $product2 both have different instant of same object and also occupy different memory.

Now take a look at the getModel Method in Mage.php:

Mage::getResourceModel()

All collections in Magento are resource models. They are instantiated by Mage::getResourceModel() or Mage::getModel()->getCollection(). It doesn’t really matter which function you use; the latter one simply calls the first one. The Magento team simply chose to make collections part of the resource, probably because collections need to query the database a lot. Usually, you will not have to call Mage::getResourceModel() for anything else than collections.

3 thoughts on “Difference between getsingleton() , getmodel() and getResourceModel() In Magento.”

Leave a Reply to Syed Cancel reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">