Magento:忽略设置的限制从产品集合获取所有产品?
我想迭代block Mage_Catalog_block_product_List_工具栏中给定的产品集合中的所有产品,忽略之前由“setPageSize()和“setCurPage()设置的限制”。我的方法如下所示:Magento:忽略设置的限制从产品集合获取所有产品?,magento,collections,Magento,Collections,我想迭代block Mage_Catalog_block_product_List_工具栏中给定的产品集合中的所有产品,忽略之前由“setPageSize()和“setCurPage()设置的限制”。我的方法如下所示: /** @var Mage_Catalog_Block_Product_List_Toolbar $this */ //... $collection = $this->getCollection(); // Remove the LIMIT and OFFSET part
/** @var Mage_Catalog_Block_Product_List_Toolbar $this */
//...
$collection = $this->getCollection();
// Remove the LIMIT and OFFSET parts from the generated SQL query:
$collection->getSelect()->reset(Zend_Db_Select::LIMIT_COUNT);
$collection->getSelect()->reset(Zend_Db_Select::LIMIT_OFFSET);
// Reload the collection using the new SQL query:
$collection->load();
foreach($collection as $product)
{
// ...
}
// ...
问题是,集合似乎没有重新加载,因此以前设置的限制仍然存在。我错过了什么?集合是否已锁定或无法更改?您在“产品列表”工具栏块中使用的集合通常已加载,并通过
Mage\u Catalog\u block\u product\u list::\u beforeHtml()
设置为工具栏实例
仅仅重置语句的计数和偏移量是不够的
此外,还需要重置属性
Varien_Data_Collection::_isCollectionLoaded
Varien_Data_Collection::_pageSize
这可以通过以下方式完成:
$collection->clear();
$collection->setPageSize(false);
在您的
重置
和加载
之间插入这些说明,您应该会没事。就是这样做的,所以集合在加载后确实会被锁定。只需清除&setPageSize,然后加载就行了。谢谢