我在自定义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秒钟。
答案 0 :(得分:2)
一个。瓶颈是您明确加载每个模型而不是直接从集合本身获取数据 - 不要获取ID,而是获取产品集合并迭代它。
B中。接下来的事情是,为什么不把制造商属性id添加为数组键,所以你不需要数组翻转。
$ manufacturers [$ product-&gt; getManufacturer()] = $产品 - &GT; getAttributeText( '制造商');
℃。更好的是构建一些自定义源模型,以简单地进行更智能的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'));
祝你好运!