Magento - When to Use Mage :: getResourceModel

I am currently studying Magento and, in particular, about how models and ORM work.

As far as I can tell, there are models (which are real entities), Resource models (which are directly connected to the database adapter) and Collections (which are containers for storing model collections).

Why then is there a ton of code and examples that are used Mage::getResourceModel()instead of just Mage::getModel()- especially when capturing a collection, i.e. Mage::getResourceModel('catalog/product_collection').

The only reason I can see is that you Mage::getModel()will have to go through the resource model at some point, so it’s more efficient to go directly to the resource model. But isn't that a bad practice?

+5
source share
2 answers

As far as I know, all collections in Magento are resource models. They are created using Mage::getResourceModel()or Mage::getModel()->getCollection(). It doesn't matter what function you use; the latter simply calls the former. The Magento team simply decided to make the collection part of the resource, probably because collections require a lot of database queries. Usually you do not need to call Mage::getResourceModel()for anything other than collections.

+6
source

Mage :: getResourceModel ()

As far as I know, all collections in Magento are resource models. They are created using

Mage :: getResourceModel ()

or

Mage :: getModel () β†’ getCollection ()

, ; . Magento , , . Mage:: getResourceModel() , .

0

All Articles