我能想到的与子查询的方式。以下摘录摘自我正在研究的图书馆。它们被放置在lib/
目录中,因此自动装载机可以访问它们。我还没有分析性能,但我的直觉告诉我,MySQL会分开缓存子查询,所以当重用时它们的结果很好而且很快。如果性能不太好,我可能不得不将这些视为数据库视图。
这种技术的关键是理解Zend的选择可以直接用作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()
因为我想不仅包含了正确的产品,但也使用店内的相应值结束。如果您的商店翻译了产品名称,则会采用正确的名称。
基本上整个事情是构建商店的所有产品列表(相当大,这就是为什么我希望它缓存),然后有效地执行“WHERE product_id IN(list_of_product_ids)”。
过滤通过商店无法正常工作(见我的帖子链接在原来的问题)。 – 2011-05-16 14:02:02
那么你只需要在你的问题中定义“正确”,或者你需要在代码级别或SQL级别中过滤这些内容 – 2011-05-16 14:49:23
我发布的链接全部说明,没有意义重复一切。基本上我在phtml模板中调用集合,并且我想为上面的集合调用添加一个商店过滤器,但首先我需要加入这些字段,以便可以访问每个产品的store_id。 – 2011-05-16 15:22:16