Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Magento查找每种产品的最深类别_Mysql_Magento_Group By - Fatal编程技术网

Mysql Magento查找每种产品的最深类别

Mysql Magento查找每种产品的最深类别,mysql,magento,group-by,Mysql,Magento,Group By,在我的产品网格页面上,我想为每种产品显示它所属的“最深”类别 这是我到目前为止提出的,基于其他一些关于类似问题的话题 $res = Mage::getSingleton('core/resource'); $eav = Mage::getModel('eav/config'); $nameattr = $eav->getAttribute('catalog_category', 'name'); $nametable = $res->getTableN

在我的产品网格页面上,我想为每种产品显示它所属的“最深”类别

这是我到目前为止提出的,基于其他一些关于类似问题的话题

    $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;
这给了我以下结果:

因此,我得到了我的产品集合,并添加了三列。显然,对于实体_id 310,“Fauteuils”是最深的类别。我现在唯一要做的就是根据实体id根据最高的cat级别对结果进行“分组”。然而,“分组方式”并没有给我期望的结果


谢谢

以下是一个简单的解决方案,您可以从中汲取灵感:

$category  = $product->getCategory();
$path      = explode('/', $category->getPath());
$deepestId = $path[count($path) - 1];

$deepestCategory = Mage::getModel('catalog/category')->load($deepestId);

Zend_Debug::dump($deepestCategory->getData());exit;

您需要查看category表中的
path
列,然后找到路径中的最后一个category ID。

谢谢,但是我在获取路径或category级别方面没有问题,您可以在我的进度(以及屏幕截图)中看到这一点。另外,加载每一个类别在性能方面也不是很明智。它完全适合我。这是一个非常好的解决方案。