Magento collection does not select attribute

Yesterday I wrote code to pull out a collection of products. This code worked fine, but today the code doo snot works, nothing has changed, I don’t understand why it will not work.

This is what I encoded

$collection = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('MPN') ->addAttributeToSelect('JAN') ->addAttributeToSelect('UPC') ->addFieldToFilter(array( array('attribute'=>'type_id','eq'=>'simple') )) ->setPage(1,10); $collection->load(); foreach($collection as $item) { echo print_r($item->getdata()); } 

Yesterday, I was able to see a list of products in which my MPN, JAN, and UPC were (these are special attributes in the backend). Today they do not load at all. This is strange, damn it, I think maybe my understanding of collections is not as good as I thought.

Any help would be greatly appreciated.


EDIT:

A magento cache update showed that everything was working again. Does this mean that the cache is sometimes used in the collection, is it possible to somehow write code so that it does not use the cache?

+6
source share
1 answer

It depends on where you use your collection. If the collection is used in the external interface and the Flat mode is enabled, your attributes will not be available, since they are not included in the Flat index.

To include them in a flat index, you need to add the following XML configuration to your module config.xml file

 <config> <frontend> <product> <collection> <attributes> <MPN /> <JAN /> <UPC /> </attributes> </collection> </product> </frontend> </config> 

In this case, it will be available in flat mode. In addition, by adding attributes to this XML node, your attributes will also be automatically added to all product list lists in the interface.

Have fun with Magento Development!

+12
source

Source: https://habr.com/ru/post/922057/


All Articles