从非产品页面获取类别中所有产品的制造商


Get manufacturers of all products in a category from non-product page

我在自定义Magento控制器中使用了以下方法来检索指定类别中的所有制造商。该模块作为一项服务来获取ajax调用的数据。

我做了很多这样的方法,所有这些方法都在我的本地服务器上以5-7秒的时间执行。此操作在本地服务器上执行需要14秒

你能帮我在这里找到一个瓶颈吗:

public function subcategoryAction() {
    $storeId = Mage::app()->getStore()->getStoreId();
    // Subcategory ID passed with a GET method
    $sub = $this->getRequest()->getParam('subcategory');
    if ($sub) {
        // Querying to get all product ID's in the specified subcategory
        $product_ids = Mage::getResourceModel('catalog/product_collection')
                        ->setStoreId($storeId)
                        ->addAttributeToFilter('status', array('eq' => '1'))
                        ->addAttributeToFilter('visibility', 4)
                        ->addCategoryFilter(Mage::getModel('catalog/category')
                                ->load($sub))->getAllIds();
        $product = Mage::getModel('catalog/product');
        // Load all the product models by their ID's
        foreach ($product_ids as $id) {
            $product->load($id);
            $manufacturers[] = $product->getAttributeText('manufacturer');
        }
        // Getting unique values of manufacurers, just like array_unique
        $manufacturers[$product->getAttributeText('manufacturer')] = $product->getAttributeText('manufacturer');
        // Echoing default option value
        echo "<option value='all'>BRAND/MAKE</option>";
        // Echoing and formatting manufacturers for a dropdown
        foreach ($manufacturers as $manufacturer) {
            if ($manufacturer != "") {
                echo "<option value='" . $manufacturer . "'>" . $manufacturer . "</option>";
            }
        }
    }
}

接受了@Mischa Leiss的建议,更改了这个混乱的唯一值代码:

$manufacturers=array_flip(array_flip(array_reverse($manufacturers,true)));

到他的代码:

$manufacturers[$product->getAttributeText('manufacturer')] = $product->getAttributeText('manufacturer');

解决方案

这是最快的解决方案,这一切都归功于@Mischa

$products = Mage::getResourceModel('catalog/product_collection')
            ->setStoreId($storeId)
            ->addAttributeToSelect('manufacturer')
            ->addAttributeToFilter('status', array('eq' => '1'))
            ->addAttributeToFilter('visibility', 4)
            ->addCategoryFilter(Mage::getModel('catalog/category')
            ->load($sub));

只需要大约2秒。

A。瓶颈是显式地加载每个模型,而不是直接从集合本身获取数据——不要获取id,而是获取产品集合并对其进行迭代。

B。接下来的问题是,为什么不添加制造商属性id作为数组键,这样就不需要数组翻转了。

$manufacturers[$product->getManufacturer()]=$product->getAttributeText("制造商");

C。更好的方法是构建一些自定义的源代码模型,以便简单地执行更智能的sql查询。

我组装了一个小的连接系列(使用颜色属性),通过产品集合获得标签/值对:

$collection = Mage::getModel('catalog/product')->getCollection();
//get the color attribute joined
$collection->addAttributeToSelect('color', 'left');
//join the label from the attribute option table
$collection->joinField('color_label', 'eav_attribute_option_value', 'value', 'option_id=color');
//group for uniqueness reset the columns and fetch what we want
$collection->getSelect()->group(array('color_label'));
$collection->getSelect()->reset(Zend_Db_Select::COLUMNS);
$collection->getSelect()->columns(array('color_label' => 'at_color_label.value', 'color_id' => 'at_color_label.option_id'));

祝你好运!