在不破坏传呼机的情况下获取第一个收集项

问题描述

| 我早些时候发布了一个有关此问题的信息,但是现在我有更多信息,我认为最好发布一个新的而不是修改(对不起,如果这不是正确的协议)。您可以在这里找到我的原始问题。 无论如何,最初的问题是我想在设置集合之后立即检查List.php类中集合中的第一项,以便我可以抓取类别并使用它来显示评论。这些都是基于自定义模块的,因此存在很多变量。此后,我在默认的Magento示例商店中尝试了此操作,仅在
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...
行,它就可以正常工作。 是什么赋予了?? 附言我知道我不应该编辑核心文件...这只是一个例子:)     

解决方法

        原因是因为在集合上调用ѭ7(或几乎任何其他检索方法)时,该集合已加载。任何后续操作都将忽略数据库并仅使用加载的数据,过滤器无效,因为它们仅是SQL,对于分页和选定的列也是如此。解决方法是使用基于第一个的第二个集合。
$secondCollection = clone $firstCollection;
$secondCollection->clear();
$_foo123 = $secondCollection->getFirstItem();
“ 9”方法将卸载该集合的数据,从而迫使其下次再次访问数据库。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...