Magento getSingleton() vs getModel() issue

前端 未结 5 1110
时光取名叫无心
时光取名叫无心 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:21

    you use Singleton when you want only one instance of an object in whole application. So when you use getSingleton it always returns the same object that can have some data loaded earlier.

    So in this case when the loop starts it create new object (of course only if this was not done before in some other place in magento) and loads the data for $product['id']. In next loop iteration it takes the same object with previous product id data and loads to it new $product['id'] data. This is why there can be some problems.

    If you want to have two different products you must use getModel.

提交回复
热议问题