我正在尝试在管理网格中显示每个产品的类别名称。我是这样做的,但它确实有效,但我不确定它是否正确:
protected function _prepareCollection()
{
/**
* Tell Magento which collection to use to display in the grid.
*/
$collection = Mage::getResourceModel(
'catalog/product_collection'
);
$collection ->addAttributeToSelect('sku')
->addAttributeToSelect('name')
->addAttributeToSelect('type_id');
$collection->joinTable(
'catalog/category_product',
'product_id=entity_id',
array('my_cat'=>'category_id'),
null,
'left'
);
$collection->joinTable(
'catalog_category_entity_varchar',
'entity_id=my_cat',
array('mv'=>'value'),
null,
'left'
);
$collection->groupByAttribute('entity_id');
$this->setCollection($collection);
return parent::_prepareCollection();
}
如果有人能告诉我这是否正确,我会很感激,并且有更好的方法吗?