Magento-joinField()查询以将门店id添加到集合中的产品

Magento-joinField()查询以将门店id添加到集合中的产品,magento,Magento,几个月来,我一直在尝试按商店根目录筛选产品集合。我尝试了许多不同的方法(,),但仍然没有找到任何有效的方法 因此,我想尝试将store_id添加到集合中的产品中。我可以使用以下方法调用集合,但我需要加入一些字段以添加存储id: $_testproductCollection = Mage::getResourceModel('catalog/product_collection'); $_testproductCollection->getSelect()->distinct(tru

几个月来,我一直在尝试按商店根目录筛选产品集合。我尝试了许多不同的方法(,),但仍然没有找到任何有效的方法

因此,我想尝试将store_id添加到集合中的产品中。我可以使用以下方法调用集合,但我需要加入一些字段以添加存储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');
谁能给我指出正确的方向吗?干杯

在核心代码中搜索:

grep '>addStoreFilter' app/code -rsn 
并遵守现有规范中的用法:

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

我可以想出一种使用子查询的方法。以下摘录摘自我正在研究的一个图书馆。它们被放置在
lib/
目录中,以便自动加载器可以访问。我还没有分析性能,但我的直觉告诉我MySQL将单独缓存子查询,这样在重用时,它们的结果又好又快。如果性能不太好,我可能不得不将它们实现为数据库视图

这种技术的关键是理解Zend的selects可以直接用作SELECT、FROM或WHERE子句,并自动成为子查询

// 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();
最后,我使用继承的
addStoreFilter()
,因为我不仅希望包含正确的产品,还希望使用商店的适当值。如果您的商店已翻译产品名称,则将采用正确的名称


基本上,整个过程是为商店构建一个所有产品的列表(相当大,这就是为什么我希望它能很好地缓存),然后有效地执行“产品id所在的位置(产品id列表)”。

好的,我认为这是可行的,没有测试太多,但似乎已经完成了这个技巧。您需要首先获取您的门店根类别id,然后加入一些字段,这样您就可以访问产品“category_id”,然后使用该id进行过滤:

$_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/>"; 
};
$\u rootcatID=Mage::app()->getStore()->getRootCategoryId();
$\u testproductCollection=Mage::getResourceModel('目录/产品\u集合')
->joinField('category_id'、'category/category_product'、'category_id'、'product_id=entity_id',null,'left')
->addAttributeToFilter('category_id',array('in'=>$\u rootcatID))
->addAttributeToSelect('*');
$\u testproductCollection->load();
foreach($\u testproductCollection作为$\u testproduct){
echo$this->htmlEscape($\u testproduct->getName())。“
”; };
按存储区进行筛选无法正常工作(请参阅原始问题中我的链接帖子)。然后,您只需在问题中定义“正确”,或者在代码级或sql级中需要在何处进行筛选。我发布的链接说明了一切,没有必要再次重复所有内容。基本上,我在phtml模板中调用集合,我想在上面的集合调用中添加一个存储筛选器,但首先我需要加入字段,以便可以访问每个产品的存储id。在编写此答案后,我使用了该技术创建了一个子查询库,其中包括一个用于的类。