1

theoretically i know about Mage::getSingleton() & differences b/w Mage::getSingleton() and Mage::getModel()

but my question is that in my custom module when to use Mage::getSingleton(){Normally i used Mage::getModel() on lots of places}.

Please let me understand with example(if possible, please share a small custom modules having Mage::getSingleton()).

1

You know the difference and others already told you,

Its hard to give example, but I will try to give some scenario.

For example you are doing some work in some page and you need to load product id 1 in block class x .

now in same page you want to use same product object again in some other block class called Y, so at that time its preferred to use singleton over getModel

because product object already loaded in class x if you reload product object then it will impact your page performance.

But if you want to load some other product id in block class y then use getModel bcuz with the use of singleton you will get old object

Hope you bit clear now

1

As per My Knowledge On Magento.

Mage::getSingleton() creates an single object for one time.

Mage::getModel() creates an object every time.

  • Tnx! for the reply. I already mentioned that theoretically, I'm aware of this. – wakar Ahamad Mar 30 '18 at 6:38
0
public static function getModel($modelClass = '', $arguments = array())
{
    return self::getConfig()->getModelInstance($modelClass, $arguments);
}



public static function getSingleton($modelClass='', array $arguments=array())
{
    $registryKey = '_singleton/'.$modelClass;
    if (!self::registry($registryKey)) {
        self::register($registryKey, self::getModel($modelClass, $arguments));
    }
    return self::registry($registryKey);
}

GetModel

Mage::getModel() will always return a new Object for the given model:

GetSingleton

Mage::getSingleton() will check whether the Object of the given model already exists and return that if it does. If it doesn't exist, it will create a new object of the given model and put in registry that it already exists. Next call will not return a new object but the existing one:

  • Tnx for the reply. I already went through it.i need some practical examples. – wakar Ahamad Mar 30 '18 at 6:41
0

Customer session model will be a great example of using getSingleton.

When you want to persist your data on that model object then you should use getSingleton.

It used to create model object at once and persist your data in memory .So that you can call using getSingleton same model into another class and then that object will have that data which was previously loaded.

For eg if you call (using getSingleton) a model inside controller and you are calling same model inside a block that is going to use on load of this controller then block will get same object that was initialize in controller.

But in case of getModel both will have separate object for same model.

0

Mage::getSingleton() = Return the same instance.

Mage::getModel() = Return a new instance.


Mage::getSingleton() :

When you use Mage::getSingleton(), Magento search in the memory if the object needed is available :

  1. If the object is available, It get it from that object every time and it will not create the object in every call.

  2. If the object is not available, so it create it one time and it return it from that new object in every call.


Mage::getModel() :

Unlike the Singleton, getModel create a new object in every call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.