问题
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->addAttributeToSelect('short_description');
$collection->addFieldToFilter('SKU',array('like' => array('%EBOOK%')));
$collection->load();
var_dump($collection->getData());
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.
回答1:
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) {
var_dump($product->getData());
}
instead of
$collection->load();
var_dump($collection->getData());
you'll get the data you're expecting.
来源:https://stackoverflow.com/questions/10304031/adding-attributes-to-collection-requests-in-magento