When to use Mage::getSingleton() in my custom module/Ext












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()).










share|improve this question





























    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()).










    share|improve this question



























      1












      1








      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()).










      share|improve this question
















      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()).







      magento-1.9 magento-1 model singleton






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 8 mins ago









      Teja Bhagavan Kollepara

      2,94841847




      2,94841847










      asked Mar 30 '18 at 3:12









      wakar Ahamadwakar Ahamad

      15711




      15711






















          4 Answers
          4






          active

          oldest

          votes


















          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






          share|improve this answer


























          • You are always welcome :) @wakarAhamad

            – Murtuza Zabuawala
            Mar 30 '18 at 6:43



















          1














          As per My Knowledge On Magento.



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



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






          share|improve this answer


























          • 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:







          share|improve this answer
























          • 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.






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "479"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f220428%2fwhen-to-use-magegetsingleton-in-my-custom-module-ext%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            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






            share|improve this answer


























            • You are always welcome :) @wakarAhamad

              – Murtuza Zabuawala
              Mar 30 '18 at 6:43
















            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






            share|improve this answer


























            • You are always welcome :) @wakarAhamad

              – Murtuza Zabuawala
              Mar 30 '18 at 6:43














            1












            1








            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






            share|improve this answer















            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







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 30 '18 at 5:25

























            answered Mar 30 '18 at 5:16









            Murtuza ZabuawalaMurtuza Zabuawala

            12.3k73360




            12.3k73360













            • You are always welcome :) @wakarAhamad

              – Murtuza Zabuawala
              Mar 30 '18 at 6:43



















            • You are always welcome :) @wakarAhamad

              – Murtuza Zabuawala
              Mar 30 '18 at 6:43

















            You are always welcome :) @wakarAhamad

            – Murtuza Zabuawala
            Mar 30 '18 at 6:43





            You are always welcome :) @wakarAhamad

            – Murtuza Zabuawala
            Mar 30 '18 at 6:43













            1














            As per My Knowledge On Magento.



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



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






            share|improve this answer


























            • Tnx! for the reply. I already mentioned that theoretically, I'm aware of this.

              – wakar Ahamad
              Mar 30 '18 at 6:38
















            1














            As per My Knowledge On Magento.



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



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






            share|improve this answer


























            • Tnx! for the reply. I already mentioned that theoretically, I'm aware of this.

              – wakar Ahamad
              Mar 30 '18 at 6:38














            1












            1








            1







            As per My Knowledge On Magento.



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



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






            share|improve this answer















            As per My Knowledge On Magento.



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



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







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 30 '18 at 6:06

























            answered Mar 30 '18 at 4:13









            Learing_CoderLearing_Coder

            664317




            664317













            • Tnx! for the reply. I already mentioned that theoretically, I'm aware of this.

              – wakar Ahamad
              Mar 30 '18 at 6:38



















            • Tnx! for the reply. I already mentioned that theoretically, I'm aware of this.

              – wakar Ahamad
              Mar 30 '18 at 6:38

















            Tnx! for the reply. I already mentioned that theoretically, I'm aware of this.

            – wakar Ahamad
            Mar 30 '18 at 6:38





            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:







            share|improve this answer
























            • Tnx for the reply. I already went through it.i need some practical examples.

              – wakar Ahamad
              Mar 30 '18 at 6:41
















            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:







            share|improve this answer
























            • Tnx for the reply. I already went through it.i need some practical examples.

              – wakar Ahamad
              Mar 30 '18 at 6:41














            0












            0








            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:







            share|improve this answer













            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:








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 30 '18 at 4:52









            Prakash PatelPrakash Patel

            1,106616




            1,106616













            • Tnx for the reply. I already went through it.i need some practical examples.

              – wakar Ahamad
              Mar 30 '18 at 6:41



















            • Tnx for the reply. I already went through it.i need some practical examples.

              – wakar Ahamad
              Mar 30 '18 at 6:41

















            Tnx for the reply. I already went through it.i need some practical examples.

            – wakar Ahamad
            Mar 30 '18 at 6:41





            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.






            share|improve this answer




























              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.






              share|improve this answer


























                0












                0








                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.






                share|improve this answer













                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 30 '18 at 6:34









                Kumar MKumar M

                66936




                66936






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f220428%2fwhen-to-use-magegetsingleton-in-my-custom-module-ext%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    What other Star Trek series did the main TNG cast show up in?

                    Berlina muro

                    Berlina aerponto