获取第一个收集项而不破坏寻呼机

Bri*_*VPS 4 magento

我之前发布了一个关于此的问题,但我现在有更多的信息,我认为最好发布一个新的而不是修改(抱歉,如果这不是正确的协议).你可以在这里找到我原来的问题.

无论如何,最初的问题是我想在设置集合之后检查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;
}
Run Code Online (Sandbox Code Playgroud)

现在,只需转到使用该类的任何产品列表(例如,类别视图),您就会明白我的意思.无论您在工具栏中的每页显示XX下选择什么,它都会始终显示列表中的所有项目.如果你注释掉那一$_foo123...行,它就可以了.

是什么赋予了??

PS我知道我不应该编辑核心文件......这只是一个例子:)

clo*_*eek 14

原因是当您getFirstItem()在集合上调用(或几乎任何其他检索方法)时,加载集合.任何后续操作都会忽略数据库并仅使用加载的数据,过滤器不起作用,因为它们只是SQL,同样适用于分页和选定的列.解决方法是使用基于第一个的第二个集合.

$secondCollection = clone $firstCollection;
$secondCollection->clear();
$_foo123 = $secondCollection->getFirstItem();
Run Code Online (Sandbox Code Playgroud)

clear()方法卸载该集合的数据,强制它下次再次访问该数据库.