Prestashop 1.6。按类别 ID 获取所有制造商

prestashop 1.6. Get all manufacturers by category ID

我需要帮助来构建正确的 SQL 查询以按类别 ID 获取与类别相关的所有制造商。或者,可能是如何实现相同结果的适当功能。去官方论坛问了,还是没有答案。 现在 - 尝试调整 this 解决方案,尽管它是为早期版本 (1.3-1.4) 构建的。找错厂商了,直接按类目ID查询,不是产品ID。使用以下内容时我得到同样的结果:

$cateMan = new Category((int)$category, $id_lang, $id_shop);
            foreach ($cateMan as $c_id) {
                $manufacturer = new Manufacturer((int)$c_id, (int)$id_lang);
                if ($manufacturer->id)
                {
                    $link = new Link;
                    $html[] = '
   <li>
    <a data-id="'.$manufacturer->id.'" 
       class="itemMenuName level1 brands-link" 
          href="'.Tools::HtmlEntitiesUTF8($link->getManufacturerLink((int)$id, $manufacturer->link_rewrite)).'">
         <span>'.$manufacturer->name.'</span>
    </a>
  </li>';
                }
            }

希望有人能帮助我。提前致谢

更新: 通过深度测试发现了一个错误——这个查询的修改版本为制造商解析了基础,但不是为所有制造商解析。例如,我有一个 ID 为 29 的类别,其中包括 ID 为 152-164、541 的子类别。但对于 ID 为 29 的类别或具有 ID 数组的子类别,也没有列出制造商。但对于另一个类别,例如 ID 为 123 的类别,它会解析制造商。 看不懂,查询有没有什么限制?

当前代码:

    $id = $category;
// query derived from category.php public static function getChildren() line 842
$subcats_query = Db::getInstance()->executeS(
                'SELECT c.`id_category`, cl.`name`, cl.`link_rewrite`, category_shop.`id_shop`
                FROM `'._DB_PREFIX_.'category` c LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category`'.Shop::addSqlRestrictionOnLang('cl').')
                '.Shop::addSqlAssociation('category', 'c').'
                WHERE `id_lang` = '.(int)$id_lang.'
                AND c.`id_parent` = '.(int)$id.'
                '.($active ? 'AND `active` = 1' : '').'
                GROUP BY c.`id_category`
                ORDER BY category_shop.`position` ASC');

// modified query function from this thread
            $subcats_query = $this->getCategoryByLevelMax($subcats_query);
            foreach ($subcats_query as $c_id) {
                $manufacturer_in_category = Db::getInstance()->executeS(
                    'SELECT p.id_manufacturer FROM `' . _DB_PREFIX_ . 'product` p
                ' . Shop::addSqlAssociation('product', 'p') . '
                LEFT JOIN `' . _DB_PREFIX_ . 'category_product` cp ON p.`id_product` = cp.`id_product`
                WHERE cp.`id_category` = ' . (int)$c_id['id_category'] . '
                AND product_shop.`visibility` IN ("both", "catalog")
                AND product_shop.`active` = 1
                GROUP BY p.id_manufacturer
            ');

                foreach ($manufacturer_in_category as $manufacturer) {
                    $manf = new Manufacturer((int)$manufacturer['id_manufacturer'], (int)$id_lang);
                $link = new Link;
                $html[] = '<li data-parent="'. $c_id['id_category'] .'"><a data-id="' . $manf->id . '" class="itemMenuName level1 brands-link" href="' . Tools::HtmlEntitiesUTF8($link->getManufacturerLink((int)$id, $manf->link_rewrite)) . '"><span>' . $manf->name . '</span></a></li>';
                }
            }

// function listed later in module. not modified. here for better understanding
public function getCategoryByLevelMax($cates = NULL) {
    if (count($cates) < 1)
        return array();
    $cateArray = array();
    foreach ($cates as $key => $cate) {
        $cate_id = $cate['id_category'];
        $cateObject = new Category((int) $cate_id);
        $cate_level = $cateObject->level_depth;
        if ($cate_level <= $this->_show_level) {
            $cateArray[$key] = $cate;
        }
    }

    if ($cateArray)
        return $cateArray;
    return array();
}

您可能会进入产品了解这一点。我猜这是一个更短且内存消耗更少的版本:

$manufacturer_in_category = Db::getInstance()->executeS('
    'SELECT p.id_manufacturer
     FROM `'._DB_PREFIX_.'product` p 
    '.Shop::addSqlAssociation('product', 'p').' 
    LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON p.`id_product` = cp.`id_product` 
    WHERE cp.`id_category` = '.(int) $id_category.' 
    AND product_shop.`visibility` IN ("both", "catalog") 
    AND product_shop.`active` = 1
    GROUP BY p.id_manufacturer
');

foreach($manufacturer_in_category as $manufacturer) {
// Here you go ...
}

请注意,我没有测试 SQL,但我非常有信心它会起作用 :)。