2013-09-01 1 views
2

다음과 같은 방법으로 사용자 정의 Magento 컨트롤러에서 지정된 카테고리의 모든 제조업체를 검색합니다. 이 모듈은 ajax 호출에 대한 데이터를 얻기위한 서비스로 만들어집니다.비 상품 페이지의 카테고리에 속한 모든 제품의 제조업체를 찾으십시오.

이렇게 많은 메소드를 만들었고 모두 로컬 서버에서 5 ~ 7 초 범위 내에서 실행됩니다. 로컬 서버에서 실행하려면 14 초이 필요합니다.

당신이 나를 여기 병목 찾는 데 도움이 될 수 :

$manufacturers[$product->getAttributeText('manufacturer')] = $product->getAttributeText('manufacturer'); 
: 자신의 코드에

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

:

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 제안을이 지저분한 고유 값 코드를 변경

해결책

이,

$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 초 정도 @Mischa

모든 덕분에 가장 빠른 솔루션입니다.

+0

감사합니다 ...하지만 가장 빠른 해결책은 100 %입니다. –

+0

@Mischa 최대한 빨리 게시하겠습니다. – l0lander

답변

2

가. 병목 현상은 컬렉션 자체에서 직접 데이터를 가져 오는 대신 명시 적으로 각 모델을로드한다는 것입니다. 즉, ID는 얻지 않고 제품 컬렉션을 가져오고 반복합니다.

B. 다음은 배열 키로 제조업체 특성 ID를 추가하면됩니다. 따라서 배열을 배열 할 필요가 없습니다.

$ 제조업체 [$ 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')); 

행운을 빕니다!

+0

감사합니다. Mischa, 당신은 나에게 숙제를주었습니다. visibility' (이전에하는 것을 잊어 버린 것들)와 실행 시간은 이제 14 초가됩니다. 또한 배열 키 부분을했고 거의 아무것도 얻지 못했지만 데이터베이스가 커지면 응답 원인의 일부로 받아 들여야합니다 ... 그러나 사용자 지정 원본 모델 **에 대한 귀하의 제안은 매우 유혹적이어서 시도 할 것입니다. 지금 당신에게 피드백과 후자의 크레딧을 줄 것입니다. 고마워요! – l0lander

+0

B. 나는'$ manufacturer-> getAttributeText ('manufacturer') = $ product-> getAttributeText ('manufacturer'); ' – l0lander

+0

을 의미한다고 가정합니다. 제 질문에 제조업체 값을 얻을 방법이 없습니다 (getAllIds뿐만 아니라) 전체 컬렉션을로드하더라도 모델을로드하고 연관된 제조업체를로드하는 데이 ID를 사용 했으므로 꽤 나 빠졌다는 것을 알 수 있습니다. – l0lander

관련 문제