Why Mage::getModel() is not using for resource model classes?
-
16-10-2019 - |
Question
This is a code snippet from app/code/core/Mage/Catalog/etc/config.xml
.
<config>
..............
<global>
<models>
<catalog>
<class>Mage_Catalog_Model</class>
<resourceModel>catalog_resource</resourceModel>
</catalog>
<catalog_resource>
<class>Mage_Catalog_Model_Resource</class>
<deprecatedNode>catalog_resource_eav_mysql4</deprecatedNode>
<entities>
<product>
<table>catalog_product_entity</table>
</product>
................
</config>
We can create Mage_Catalog_Model_Porduct object as
$model = Mage::getModel('catalog/product');
Same way can create the resource model object as follows.
$resourceModel = Mage::getModel('catalog_resource/product');
I know basically there's already a function as Mage::getResourceModel();
.
Example: Mage::getResourceModel('catalog/product');
But this way is also works fine.
Can anyone explain why not using Mage::getModel()
to get the resource model object ? Any suggestions will be appreciated.
Solution
This is just my opinion so feel free to challange it, but I thing it's because in theory you can change the resource model class. For example I can add in my own module this:
<global>
<models>
<catalog>
<resourceModel>my_resource</resourceModel>
</catalog>
<my_resource>
<class>My_Module_Model_Resource</class>
....
If I were to use Mage::getModel('catalog_resource/product')
I would get and instance of the original class because in the config DOM the declaration still exists.
but by calling Mage::getResourceModel('catalog/product')
now I get and instance of my new class.
So I can conclude that the reason it 'extensibility'.
An other reason could be to keep a consistency.
If you call Mage::getModel($someParam)
you will get the corresponding resource model by using the same parameter : $someParam
.