We will be looking into some important methods used frequently in Magento like getModel(), getData(), getSingleton()..
getModel() (To create an instance of a Model class) :
The getModel() function is used to create an instance of a Model class. For example
This function is used to get relevant data from the object instance. Let’s say you want to retrieve the SKU value. You can use some other method for that like getSku() method that needs to be executed on object of Product type or you can:
First use the getModel to create the instance of object then you use getData to retrieve the data from that instance.
getSingleton():
One of the important architectural feature is it’s Singleton design pattern. In short, Singleton design pattern ensures a class has only one instance. Therefore one should provide a global point of access to that single instance of a class.
So when you are using getSingleton you are calling already instantiated object. So if you get the empty array as a result, it means the object is empty. Only the blueprint is there, but nothing is loaded in it.
Registry:
We had told that Magento provides global point of access to single instance of classes. How does Magento do it ?. This is done by using an array called registry in Mage.php.
Checkout the Mage.php file and you will see an array variable called _registry.
In order to provide global point of access for an instance of a class we must register the object into the registry array by using a function called register. For example:
Here is the code of the register() function:
Whenever we want to call an object from the registry we use the registry() function. For example in the getConfig() method:
The getModel() and getSingleton() functions works by using this registry. Here is the code for the getModel() function:
Now take a look at the getSingleton Method:
getModel() (To create an instance of a Model class) :
The getModel() function is used to create an instance of a Model class. For example
would basically tell Magento to create an instance of Mage_Catalog_Model_Product class. (ie. Mage/Catalog/Model/Product.php). If we now echo the get_class($Product) we would get the Mage_Catalog_Model_Product text displayed in our browser. You can use all methods defined in Product.php. Some useful methods include getName, getPrice, getTypeId, getStatus which we can execute like:
1
$Product
= Mage::getModel(’catalog/product’);
However, the above code wont give you anything. You will not see any price displayed in the browser. This is because you haven’t loaded the product using load() method. We have to use the load method to load some data into our newly instantiated object.
1
echo
‘Product name: ‘.
$Product
->getName();
2
echo
‘Product price: ‘ .
$Product
->getPrice();
getData() (To get relevant data from the object instance)where 53 is the product id.
1
$Product
->load(53);
This function is used to get relevant data from the object instance. Let’s say you want to retrieve the SKU value. You can use some other method for that like getSku() method that needs to be executed on object of Product type or you can:
getData can be executed with or without any parameters passed to it. If you execute it without any parameters you get the array variable as a result. You can’t directly echo the array. You would have to map it to some field, like echo $arayVar['someField']. What are all the available fields? To find out you can do something like
1
echo
$Product
->getData(’sku’);
You can get the sku of the product by:
1
echo
‘<pre>’;
2
print_r(
$Product
->getData());
3
echo
‘</pre>’;
or:
1
$ProductData
=
$Product
->getData();
2
echo
$ProductData
->sku;
Another example:
1
echo
$Product
->getData(’sku’)
Now your $StockItem variable is a object of type Mage_CatalogInventory_Model_Stock_Item and you can go ahead and use the same principle used until this point to find it’s methods and the data it holds.
1
$ProductData
=
$Product
->getData();
2
$StockItem
=
$ProductData
->stock_item;
First use the getModel to create the instance of object then you use getData to retrieve the data from that instance.
getSingleton():
One of the important architectural feature is it’s Singleton design pattern. In short, Singleton design pattern ensures a class has only one instance. Therefore one should provide a global point of access to that single instance of a class.
So when you are using getSingleton you are calling already instantiated object. So if you get the empty array as a result, it means the object is empty. Only the blueprint is there, but nothing is loaded in it.
Registry:
We had told that Magento provides global point of access to single instance of classes. How does Magento do it ?. This is done by using an array called registry in Mage.php.
Checkout the Mage.php file and you will see an array variable called _registry.
In order to provide global point of access for an instance of a class we must register the object into the registry array by using a function called register. For example:
1 | Mage::register(’events’, new Varien_Event_Collection()); |
2 | Mage::register(’config’, new Mage_Core_Model_Config()); |
Basically what it does it registers an object with name as $key and the instance as $value into the registry array. You can also unregister an object using the unregister() function.
1
public
static
function
register(
$key
,
$value
,
$graceful
= false)
2
{
3
if
(isset(self::
$_registry
[
$key
])) {
4
if
(
$graceful
) {
5
return
;
6
}
7
Mage::throwException(’Mage registry key “‘.
$key
.’” already exists’);
8
}
9
self::
$_registry
[
$key
] =
$value
;}
Whenever we want to call an object from the registry we use the registry() function. For example in the getConfig() method:
Previously we had registered an instance of Mage_Core_Model_Config as ‘config’. The getConfig() function returns that instance. Here is the code for registry() function:
1
public
static
function
getConfig()
2
{
3
return
Mage::registry(’config’);
4
}
Analyzing the getModel() and getSingleton() code:
1
public
static
function
registry(
$key
)
2
{
3
if
(isset(self::
$_registry
[
$key
])) {
4
return
self::
$_registry
[
$key
];
5
}
6
return
null;
7
}
The getModel() and getSingleton() functions works by using this registry. Here is the code for the getModel() function:
Basically it gets the Config object (This object is formed by parsing all the config.xml files, so it contains all config details of all moules) and then makes an instance of the model that is specified by the $modelClass argument.(for example if $modelClass is ‘catalog/product it creates an instance of Mage_Catalog_Model_Product)
1
public
static
function
getModel(
$modelClass
=”,
$arguments
=
array
())
2
{
3
return
Mage::getConfig()->getModelInstance(
$modelClass
,
$arguments
);
4
}
Now take a look at the getSingleton Method:
Basically it checks wheter an instance of that class is already existing, if so return that instance else create a new instance and register it as ‘_singleton/$modelClass’.
1
public
static
function
getSingleton(
$modelClass
=”,
array
$arguments
=
array
())
2
{
3
$registryKey
= ‘_singleton/’.
$modelClass
;
4
if
(!Mage::registry(
$registryKey
)) {
5
Mage::register(
$registryKey
, Mage::getModel(
$modelClass
,
$arguments
));
6
}
7
return
Mage::registry(
$registryKey
);
8
}
No comments:
Post a Comment