Magento - joinField() 查询将 store_id 添加到集合中的产品
几个月来我一直在尝试按产品集合的商店根类别来过滤产品集合。我尝试了多种不同的方法( Magento - addStoreFilter 不起作用? 、 Magento ->addCategoryFilter - 按根类别过滤产品集合 , Magento - 加载根集合加载所有产品)但仍然没有找到任何有效的东西。
所以我想尝试将 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');
有人能指出我正确的方向吗?干杯!
I've been trying for months now to filter a product collection by it's store root category. I've tried numerous different ways ( Magento - addStoreFilter not working? , Magento ->addCategoryFilter - filtering product collection by root category , Magento - Loading root collection loads all products ) but still haven't found anything that works.
So I want to try adding in the store_id to the products in a collection. I can call the collection using the following, but I need to join some fields in order to add the store_id:
$_testproductCollection = Mage::getResourceModel('catalog/product_collection');
$_testproductCollection->getSelect()->distinct(true);
$_testproductCollection->addAttributeToSelect('*')->load();
The joinField I need is something like the following (which was given in a post linked above) but the store_id=1 part changes every products store_id to 1, which I don't want, I just want a way to add it's designated store so I can then filter by it.
$_testproductCollection->joinField('store_id', 'catalog_category_product_index', 'store_id', 'product_id=entity_id', '{{table}}.store_id = 1', 'left');
Can anyone point me in the right direction? Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可以想出一种使用子查询的方法。以下摘录摘自我正在开发的一个图书馆。它们被放置在
lib/
目录中,以便自动加载器可以访问。我还没有分析性能,但我的直觉告诉我 MySQL 会单独缓存子查询,因此当重用时,它们的结果又好又快。如果性能不太好,我可能必须将它们实现为数据库视图。这项技术的关键是理解 Zend 的 select 可以直接用作 SELECT、FROM 或 WHERE 子句,并自动成为子查询。
接近尾声时,我使用继承的
addStoreFilter()
因为我不仅希望包含正确的产品,还希望使用商店的适当值。如果您的商店有翻译的产品名称,系统将采用正确的名称。基本上整个事情就是构建商店所有产品的列表(相当大,这就是为什么我希望它缓存得很好),然后有效地执行“WHERE Product_id IN (list_of_product_ids)”。
I can think of a way with subqueries. The following excerpts are taken from a library I'm working on. They are placed in the
lib/
dir so are accessible to the autoloader. I haven't yet profiled the performance but my gut tells me MySQL will cache the subqueries separately so when reused their results are nice and fast. I may have to implement these as database views if the performance isn't so good.The key to this technique is understanding that Zend's selects can be used directly as SELECT, FROM or WHERE clauses and automatically become a sub-query.
Near the end I use the inherited
addStoreFilter()
because I want to not only include the correct products but use the store's appropriate values too. If your stores have translated product names this will adopt the correct one.Basically the entire thing is constructing a list of all products for the store (quite big which is why I hope it caches well) then effectively does "WHERE product_id IN (list_of_product_ids)".
在核心代码中搜索:
并观察现有代码中的用法:
search in core code:
and observe usages in existing code:
好吧,我认为这可行,没有测试太多,但似乎已经成功了。您需要首先获取商店根类别 id,然后加入一些字段,以便您可以访问产品“category_id”,然后使用它进行过滤:
OK, I think this works, haven't tested too much but seems to have done the trick. You need to first get your stores root category id, then join some fields so you have access to the products "category_id", then filter using that: