adding attributes to collection requests in magento

adding attributes to collection requests in magento  using -'php,magento'

I hope someone can help me puzzle this one out. I'm trying to load some data out of a Magento catalog model using a collection. The code looks like this:

$model = Mage::getModel('catalog/product');
$collection = $model->getCollection();
$collection->addFieldToFilter('SKU',array('like' => array('%EBOOK%')));

This produces a dump of objects with all the fields in the flat catalog product table, but not the field that I have requested with the $collection->addAttributeToSelect() method. No matter what field I specify with this method (even '*'), I cannot get the collection to return anything other than its standard set of fields. I also can't unset any fields using $collection->removeFieldFromSelect(NULL) which is supposed to work.

Am I doing something stupid/wrong/both?

Thanks in advance.

asked Oct 11, 2015 by nimisha.jagtap
0 votes

1 Answer

0 votes

This is because you call getData() on the collection, but not on a product of this collection.

I never really analyzed why this happens, but if you use

foreach ($collection as $product) {

instead of


you'll get the data you're expecting.

answered Oct 11, 2015 by kinnari