2

我在自定义 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 秒。

4

1 回答 1

2

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

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

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

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'));

祝你好运!

于 2013-09-01T14:29:40.227 回答