私の製品グリッドページで、製品ごとに、それが配置されている「最も深い」カテゴリを表示したいと思います。
これは、同様の質問に関する他のいくつかのトピックに基づいて、これまでに思いついたものです。
$res = Mage::getSingleton('core/resource');
$eav = Mage::getModel('eav/config');
$nameattr = $eav->getAttribute('catalog_category', 'name');
$nametable = $res->getTableName('catalog/category') . '_' . $nameattr->getBackendType();
$nameattrid = $nameattr->getAttributeId();
$collection
->joinTable('catalog/category_product',
'product_id=entity_id', array('single_category_id' => 'category_id'),
null,'left')
->joinTable('catalog_category_entity',
"entity_id=single_category_id", array('cat_level' => 'level','cat_path' => 'path'),
null,'left')
//->addAttributeToSort('entity_id', 'ASC')
//->addAttributeToSort('cat_level', 'ASC')
//->groupByAttribute('entity_id')
->joinTable($nametable,
"entity_id=single_category_id", array('single_category_name' => 'value'),
"attribute_id=$nameattrid", 'left')
->printLogQuery(true);exit;
これにより、次の結果が得られます。
だから私は私の製品コレクションを手に入れ、3つの列が追加されました。明らかに、entity_id 310の場合、「Fauteuils」が最も深いカテゴリです。私が今しなければならない唯一のことは、最高のcat_levelに基づいてentity_idによって結果を「グループ化」することです。ただし、「groupby」では目的の結果が得られません。
ありがとう