我之前发布了一个关于此问题的问题,但我现在有了更多信息,我认为最好发布一个新问题而不是修改(抱歉,如果这不是正确的协议)。你可以找到我原来的问题here https://stackoverflow.com/questions/6311646/magento-collection-catch-22/.
无论如何,最初的问题是我想在设置集合后检查 List.php 类中集合中的第一个项目,以便我可以抓取类别并使用它来显示评论。这一切都是基于自定义模块,因此存在很多变量。我已经在默认的 Magento 示例商店中尝试过,并且只添加了ONE线路到app/code/core/Mage/catalog/Block/Product/List.php
破坏寻呼机。以下是详细信息。如果您有任何想法为什么会发生这种情况,请告诉我,因为我被困住了
首先,打开app/code/core/Mage/catalog/Block/Product/List.php
并找到_getProductCollection
功能。结束时if (is_null...)
阻止、添加$_foo123 = $this->_productCollection->getFirstItem();
所以你有一个如下所示的函数:
protected function _getProductCollection()
{
if (is_null($this->_productCollection)) {
$layer = $this->getLayer();
/* @var $layer Mage_Catalog_Model_Layer */
if ($this->getShowRootCategory()) {
$this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
}
// if this is a product view page
if (Mage::registry('product')) {
// get collection of categories this product is associated with
$categories = Mage::registry('product')->getCategoryCollection()
->setPage(1, 1)
->load();
// if the product is associated with any category
if ($categories->count()) {
// show products from this category
$this->setCategoryId(current($categories->getIterator()));
}
}
$origCategory = null;
if ($this->getCategoryId()) {
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
}
}
$this->_productCollection = $layer->getProductCollection();
$this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
if ($origCategory) {
$layer->setCurrentCategory($origCategory);
}
//THIS LINE BREAKS THE PAGER
$_foo123 = $this->_productCollection->getFirstItem();
}
return $this->_productCollection;
}
现在,只需转到使用该类的任何产品列表(例如类别视图),您就会明白我的意思。无论您在下面选择什么每页显示 XX在工具栏中,它将始终显示列表中的所有项目。如果你注释掉$_foo123...
线,效果很好。
是什么赋予了??
附:我知道我不应该编辑核心文件...这只是一个例子:)
原因是因为当你打电话时getFirstItem()
(或任何其他检索方法)在集合上loaded。任何后续操作都会忽略数据库并仅使用加载的数据,过滤器不起作用,因为它们仅是 SQL,分页和选定列也是如此。解决方法是使用基于第一个集合的第二个集合。
$secondCollection = clone $firstCollection;
$secondCollection->clear();
$_foo123 = $secondCollection->getFirstItem();
The clear() http://docs.magentocommerce.com/Varien/Varien_Data/Varien_Data_Collection.html#clear method unloads该集合的数据,迫使它下次再次访问数据库。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)