我正在尝试加载与产品关联的所有类别,但只应选择最深的子类别。例如,产品与类别相关联,women > trousers > jeans
并且只jeans
应该是可见的。
到目前为止我想出的代码返回了正确的查询,但是在访问集合时抛出了一个 sql 错误:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cat_path' in 'on clause'
/* @var $_categoryNames Mage_Catalog_Model_Resource_Category_Collection */
$_categoryNames = $_product->getCategoryCollection();
$_categoryNames
->joinTable(
'catalog/category',
"entity_id = entity_id",
array('cat_path' => 'path')
)
->getSelect()
->where(
"cat_path not like( CONCAT( path, '/%' ) )"
);
die($_categoryNames->getSelect());
更新: 感谢 blmage,我至少能够找到一条 sql 语句来获得正确的类别:
SELECT *
FROM `catalog_category_entity`e
WHERE (SELECT COUNT(`entity_id`)
FROM `catalog_category_entity`c
WHERE c.path LIKE (CONCAT(e.path,'%'))
)=1