0

我正在尝试加载与产品关联的所有类别,但只应选择最深的子类别。例如,产品与类别相关联,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
4

2 回答 2

1

我现在的解决方案使用子选择而不是连接。可能还有一些需要改进的地方,但它对我有用。

 $_categoryIds = $_product->getCategoryIds();


/** @var $read Varien_Db_Adapter_Interface */
$read = Mage::getSingleton('core/resource')
    ->getConnection('core_read');

$_categoryIdSelect=$read->select()
    ->from(
        array(
            'c' => $_categories->getTable('catalog/category')
        ),
        'COUNT(e.entity_id)'
    )
    ->where('entity_id IN (?)', $_categoryIds)
    ->where(
        "c.path LIKE (CONCAT(e.path,'%'))"
    );

 /* @var $_categoryNames Mage_Catalog_Model_Resource_Category_Collection */
$_categoryNames = $_product->getCategoryCollection();       
$_categoryNames->getSelect()
            ->where(
                '2 > (?)',
                new Zend_Db_Expr($_categoryIdSelect)
            );
于 2013-07-03T08:01:56.043 回答
0

尝试这个

$path = "/abc/example";

$_categoryNames->addFieldToFilter('cat_path', array('nlike' => $path));
于 2013-06-28T14:59:55.037 回答