28

I just started to use Magento, and in particular on how models and the ORM work.

I have used these three methods

Mage::getResourceModel()
Mage::getModel()
Mage::getSingleton()

Can anyone tell me what the difference is between each of them?

I have found that getSingleton() shares memory while getModel() uses fresh memory for new object for the same table being loaded.

I have used all the methods above but could not differentiate between them and when is appropriate to use which one.

dayuloli
  • 16,205
  • 16
  • 71
  • 126
user3177068
  • 557
  • 2
  • 5
  • 13

2 Answers2

47

Perfect differece with example for getsingleton and getmodel.

Mage::getSingleton()

Mage::getSingleton() will first check if the same class instance exists or not in the memory. If the instance exists then it will return the same object from the memory. So Mage::getSingleton() is faster than Mage::getModel().

Example

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

$product1 and $product2 both will share same memory of OS and return only one instance each time.

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.

good post by balajimca

ahnbizcad
  • 10,491
  • 9
  • 59
  • 85
Sam
  • 1,106
  • 10
  • 14
11

Also if you know what attributes you need then using Mage::getResourceModel with filters is ~5x more efficient in both speed and memory than loading via say Mage::getModel('catalog/product').

e.g. getResourceModel method of retrieving a product from the db

$collection = Mage::getResourceModel('catalog/product_collection')
    ->addFieldToFilter('entity_id', array($productId))
    ->addAttributeToSelect(array('name'))
    ->setPageSize(1);
$product = $collection->getFirstItem();

load a model (this will load all eav attributes)

Mage::getModel('catalog/product')->load($productId);

[more info here][1] http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1

Ben Incani
  • 416
  • 3
  • 10
  • This helped, but I had to use `->addFieldToSelect('name_of_field')` on a non-product based concrete class extending `Mage_Core_Model_Resource_Db_Collection_Abstract` – Leo Fisher Jan 25 '21 at 19:44
  • 1
    @LeoFisher yes, you need to use addFieldToSelect([]) for non EAV collections – Ben Incani Feb 01 '21 at 00:47