获取产品的类别 ID 和名称 [英] Get product's category ids and names

查看:28
本文介绍了获取产品的类别 ID 和名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There is this method getCategoryIds() in Mage_Catalog_Model_Resource_Eav_Mysql4_Product. This method returns all category IDs of requested product. I need to modify the SELECT statement so it will return also category names.

这是基本查询:


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

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

I can't use catalog_category_flat table to for some reasons, so I have to use EAV tables. So at this point I have this query:


$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'");

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

This works, but I'd like to ask if there is a better way of doing it.

推荐答案

最简单也可能最干净:

$categories = $product->getCategoryCollection()
    ->addAttributeToSelect('name');

然后就可以简单的遍历集合了:

Then you can simply traverse the collection:

foreach($categories as $category) {
    var_dump($category->getName());
}

这篇关于获取产品的类别 ID 和名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆