Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - When to use Mage::getResourceModel

Tags:

magento

I am learning Magento currently and in particular how models and the ORM works.

As far as I can make out there are Models (which are the actual entities), Resource Models (which links directly with the database adapter) and Collections (which are containers to hold collections of models).

Why then, is there a ton of code and examples that use the Mage::getResourceModel() instead of just Mage::getModel() - particularly when grabbing a collection i.e. Mage::getResourceModel('catalog/product_collection').

The only reason I can see would be that Mage::getModel() would have to go through the resource model at some point so it may be more efficient to go directly to the resource model. However, isnt this bad practice?

like image 552
dwerner Avatar asked Nov 11 '11 21:11

dwerner


1 Answers

As far as I know, all collections in Magento are resource models. They are instantiated by Mage::getResourceModel() or Mage::getModel()->getCollection(). It doesn't really matter which function you use; the latter one simply calls the first one. The Magento team simply chose to make collections part of the resource, probably because collections need to query the database a lot. Usually, you will not have to call Mage::getResourceModel() for anything else than collections.

like image 186
Anders Thirsgaard Rasmussen Avatar answered Oct 02 '22 08:10

Anders Thirsgaard Rasmussen