2011-05-16 3 views
1

저는 상점 루트 카테고리별로 제품 컬렉션을 필터링하기 위해 몇 달 동안 노력해 왔습니다. 여러 가지 방법 (Magento - addStoreFilter not working?, Magento ->addCategoryFilter - filtering product collection by root category, Magento - Loading root collection loads all products)을 시도했지만 여전히 작동하는 것을 찾지 못했습니다.컬렉션의 제품에 store_id를 추가하는 Magento - joinField() 쿼리

그래서 컬렉션의 제품에 store_id를 추가하려고합니다. 나는 다음을 사용하여 컬렉션을 호출 할 수 있습니다,하지만 난 STORE_ID 추가하기 위해 일부 필드에 가입해야합니다 :

$_testproductCollection = Mage::getResourceModel('catalog/product_collection'); 
$_testproductCollection->getSelect()->distinct(true); 
$_testproductCollection->addAttributeToSelect('*')->load(); 

내가 필요로하는 joinField (위 링크 된 게시물에 주어진) 다음과 같은 뭔가입니다 만 store_id = 1 부분은 모든 제품 store_id를 1로 변경합니다. 원하지 않는 항목은 지정된 상점을 추가하여 원하는 항목으로 필터링 할 수 있습니다.

$_testproductCollection->joinField('store_id', 'catalog_category_product_index', 'store_id', 'product_id=entity_id', '{{table}}.store_id = 1', 'left'); 

누구나 올바른 방향으로 나를 가리킬 수 있습니까? 건배! 핵심 코드에

답변

0

좋아요,이게 효과가 있다고 생각합니다. 너무 많이 테스트하지는 않았지만 트릭을 완료 한 것으로 보입니다. 내 링크 된 게시물을 참조 (제대로 작동하지 않습니다 저장소에서

$_rootcatID = Mage::app()->getStore()->getRootCategoryId(); 

$_testproductCollection = Mage::getResourceModel('catalog/product_collection') 
->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left') 
->addAttributeToFilter('category_id', array('in' => $_rootcatID)) 
->addAttributeToSelect('*'); 
$_testproductCollection->load(); 

foreach($_testproductCollection as $_testproduct){ 
    echo $this->htmlEscape($_testproduct->getName())."<br/>"; 
}; 
0

검색 :

grep '>addStoreFilter' app/code -rsn 

기존 코드의 용도를 준수하십시오

Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addStoreFilter(Mage::app()->getStore()->getId()); 
+0

필터링 : 당신은 그를 사용하여 필터링 먼저, 당신의 상점 루트 카테고리 ID를 얻을 당신이 "CATEGORY_ID"제품에 액세스 할 수 있도록 일부 필드에 가입해야 원래 질문에서). –

+0

다음 질문에 "올바르게"정의해야합니다 또는 코드 수준 또는 SQL 수준에서 필터링 할 필요가 –

+0

내가 게시 한 링크는 모든 것을 말하고, 모든 점을 다시 반복하지 않습니다. 기본적으로 필자는 phtml 템플릿으로 콜렉션을 호출하고 위의 콜렉션 호출에 상점 필터를 추가하고 싶지만 먼저 각 제품의 store_id에 액세스 할 수 있도록 필드에 가입해야합니다. –

2

나는 서브 쿼리와 방법을 생각할 수있다. 다음 발췌 내용은 내가 작업하고있는 도서관에서 발췌 한 것입니다. 이 파일들은 lib/ 디렉토리에 있으므로 오토로더에서 액세스 할 수 있습니다. 아직 성능을 프로파일 링하지는 못했지만, MySQL은 하위 쿼리를 개별적으로 캐싱 할 것이므로 재사용하면 결과가 멋지고 빠릅니다. 성능이 좋지 않으면 데이터베이스 뷰로 구현해야 할 수도 있습니다.

Zend의 선택을 SELECT, FROM 또는 WHERE 절로 직접 사용할 수 있고 자동으로 하위 쿼리가 될 수 있다는 것을 이해해야합니다. 내가 올바른 제품을뿐만 아니라 포함하지만 너무 가게의 적절한 값을 사용하려는 때문에 상속 된 addStoreFilter()을 사용하여 끝 부분

// A shortened example, not tested directly 
class Example_Product_List extends Varien_Db_Select { 

    protected $_resource; 

    public function __construct($storeId) { 
     // Since this isn't a collection we need to get the resource ourselves 
     $this->_resource = Mage::getSingleton('core/resource'); 
     $adapter = $this->_resource->getConnection('core_read'); 
     parent::__construct($adapter); 

     $this->from(
      array('catprod'=>$this->getTable('catalog/category_product')), 
      'product_id' 
     ); 
     $this->joinInner(
      array('catprodin'=>$this->getTable('catalog/category_product_index'), 
      '(catprodin.category_id=catprod.category_id) AND (catprodin.product_id=catprod.product_id)', 
      'store_id' 
     ); 
     $this->where('store_id = ?', $storeId); 
     $this->group('product_id'); 
    } 

    public function getTable($modelEntity) 
    { 
     return $this->_resource->getTableName($modelEntity); 
    } 

} 

// Using it in a collection 
class Example_Module_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4 { 

    protected function setStoreId($store) { 
     $this->getSelect()->where(array(
      'attribute'=>'product_id', 
      'in'=>new Example_Product_list($store->getId()) 
     ); 
     return parent::setStoreId($store); 
    } 
} 

// Putting it to use, '1' can be any store ID. 
$_testproductCollection = Mage::getResourceModel('examplemodule/product_collection'); 
$_testproductCollection->addStoreFilter(1); 
$_testproductCollection->addAttributeToSelect('*')->load(); 

. 상점에서 제품 이름을 번역하면 올바른 이름이 적용됩니다.

기본적으로 모든 것이 저장소의 모든 제품 목록을 구성하고 있습니다 (캐시가 잘되기를 바란 이유입니다). 그러면 "where product_id IN (list_of_product_ids)"가 효과적으로 수행됩니다.

+0

이 답변을 작성한 이래로 저는 [쿼리 패턴] (https://github.com/Knectar/Magento-Query-Patterns)을 만드는 기술을 사용했습니다. 하위 쿼리 라이브러리 - [매핑 저장소 제품] (https : // github.com/Knectar/Magento-Query-Patterns/blob/master/Knectar/선택/저장/카테고리/Product.php). – clockworkgeek

관련 문제