Magento getSingleton() vs getModel() issue

前端 未结 5 1109
时光取名叫无心
时光取名叫无心 2020-12-24 03:30

I want to loop over an array of product IDs in Magento. In the loop I am displaying some custom attributes of the products as:

foreach ($products as $product         


        
5条回答
  •  太阳男子
    2020-12-24 04:02

    First of all I would like to explain the difference between Mage::getSingleton() and Mage::getModel() functions.

    When you call the function Mage::getSingleton('catalog/product') magento will search in the memory whether there is any object available. If not it will create a new object for Mage_catalog_Model_product class. In the first iteration of the foreach loop this happens. But from the second iteration when magento searches in memory for Mage_catalog_Model_product class object it will find the object which was called in the first iteration. So magento won't create any new object and instead it will call the same object which is already in the memory.

    BUT,

    If you use Mage::getModel('catalog/product) this function always creates new object of Mage_catalog_Model_product class in the memory whenever you called it. So in the loop this function will create one object per iteration.

提交回复
热议问题