4

Which one is more correct:
Mage::getResourceModel() or
Mage::getModel('catalog/product')->getCollection()

I always use the first method unless I already have a model that I can call the getCollection on. I'm just wondering if there are any advantages/disadvantages to it.

5

The methods Mage_Core_Model_Abstract::getResource() and Mage_Core_Model_Abstract::getCollection() are just handy shortcuts if you want to get the resource model or collection for an existing model instance.

They eventually call Mage::getResourceModel(), so if you don't already have an instantiated model, the more straightforward way to instantiate a collection or resource model is to use Mage::getResourceModel() directly. The result is always the same.

5

Mage::getModel()

Mage::getModel() will create a new instance of an object each time even such object exists in configuration.

Example

$product1 = Mage::getModel('catalog/product');
$product2 = Mage::getModel('catalog/product');

$product1 and $product2 both have different instant of same object and also occupy different memory

Mage::getResourceModel()

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.

Reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.