Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between getSingleton() and getModel()

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 265
    Comment on it

    Mage::getSingleton() :

     

    The getSingleton() method first checks for existence of the instance of the same class in the memory. If it finds the instance then it will return the same object from the memory. and if not than it will create a new instance of the class.

    This method is useful for say if we have to create the instance of the database connection. Why would we make object references every time we have to perform connection from starting to avoid this the reference to the object which was already instantiated is returned using getSingleton() method. It also avoids the integrity problem.

     

    Mage::getModel() :

     

    The Mage::getModel() is faster than getSingleton().

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

    getModel will always return a new instance of the requested model every time.

     

    For example:- 

     

    $product = Mage::getSingleton('catalog/product');
    $product1 = Mage::getSingleton('catalog/product');

    The blueprint of the both function in the Mage.php is as :

    • getSingleton()
    public static function getSingleton($modelClass='', array $arguments=array())
    {
      $registryKey = '_singleton/'.$modelClass;
      if (!Mage::registry($registryKey)) {
        Mage::register($registryKey, Mage::getModel($modelClass, $arguments));
      }
      return Mage::registry($registryKey);
    }
    • getModel() 
    public static function getModel($modelClass='', $arguments=array())
    {
      return Mage::getConfig()->getModelInstance($modelClass, $arguments);
    }


    We can notice that static keyword of OOPs concept that shows the function can be called with non object. Both have the same parameter list in the brackets. But the getSingleton() method returns the already instantiated object of the model whereas the getModel() always creates the new object. so, if we get the empty array in getSingleton() it means the object is empty and in getModel() method whenever we print the array with print_r or var_dump we will receive the empty array if the object contains the blueprint only.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: