When to use Mage::getSingleton() in my custom module/Ext
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
add a comment |
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
add a comment |
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
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
magento-1.9 magento-1 model singleton
edited 8 mins ago
Teja Bhagavan Kollepara
2,94841847
2,94841847
asked Mar 30 '18 at 3:12
wakar Ahamadwakar Ahamad
15711
15711
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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
usegetModel
bcuz with the use ofsingleton
you will get old
object
Hope you bit clear now
You are always welcome :) @wakarAhamad
– Murtuza Zabuawala
Mar 30 '18 at 6:43
add a comment |
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
add a comment |
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
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
usegetModel
bcuz with the use ofsingleton
you will get old
object
Hope you bit clear now
You are always welcome :) @wakarAhamad
– Murtuza Zabuawala
Mar 30 '18 at 6:43
add a comment |
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
usegetModel
bcuz with the use ofsingleton
you will get old
object
Hope you bit clear now
You are always welcome :) @wakarAhamad
– Murtuza Zabuawala
Mar 30 '18 at 6:43
add a comment |
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
usegetModel
bcuz with the use ofsingleton
you will get old
object
Hope you bit clear now
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
usegetModel
bcuz with the use ofsingleton
you will get old
object
Hope you bit clear now
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
As per My Knowledge On Magento.
Mage::getSingleton() creates an single object for one time.
Mage::getModel() creates an object every time.
As per My Knowledge On Magento.
Mage::getSingleton() creates an single object for one time.
Mage::getModel() creates an object every time.
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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:
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:
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 30 '18 at 6:34
Kumar MKumar M
66936
66936
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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