获取产品的类别ID和名称

Ven*_*tus 2 magento

目前这种方法getCategoryIds()Mage_Catalog_Model_Resource_Eav_Mysql4_Product.此方法返回所请求产品的所有类别ID.我需要修改SELECT语句,以便它还返回类别名称.

这是基本查询:


$select = $this->_getReadAdapter()->select()
    ->from($this->_productCategoryTable, 'category_id')
    ->where('product_id=?', $product->getId());
Run Code Online (Sandbox Code Playgroud)

catalog_category_flat由于某些原因我不能使用表,所以我必须使用EAV表.所以在这一点上我有这个查询:


$select = $this->_getReadAdapter()->select()
    ->from($this->_productCategoryTable, 'category_id')
    ->where('catalog_category_product.product_id=?', $product->getId())
    ->join(
       array('a' =>'catalog_category_entity_varchar'),
       'a.entity_id = catalog_category_product.category_id',
       array('name' => 'value')
    )
    ->join(
       array('b' => $this->getTable('eav/attribute')),
       'b.attribute_id = a.attribute_id',
       array()
    )
    ->where("b.attribut_code = 'name'");
Run Code Online (Sandbox Code Playgroud)

这有效,但我想问一下是否有更好的方法.

Dan*_*oof 9

最容易也可能最干净:

$categories = $product->getCategoryCollection()
    ->addAttributeToSelect('name');
Run Code Online (Sandbox Code Playgroud)

然后你可以简单地遍历集合:

foreach($categories as $category) {
    var_dump($category->getName());
}
Run Code Online (Sandbox Code Playgroud)