Magento Collection Catch-22

时间:2011-06-10 20:18:15

标签: magento

我真的不明白Magento系列是如何工作的,所以希望这是一个简单的问题......

正如古老的谚语所说,你不能以某种方式改变它来观察实验。这似乎适用于Magento系列。我有一个特色产品模块,我写的很好用。我们最近在商店中添加了客户评论。查看类别页面时,它会显示该类别中产品的随机评论。这也很有效。我将评论块添加到我的特色产品页面,这很容易做到,但由于这些产品不属于特定类别,因此它只是提取一个随机的,通常不相关的评论。为了解决这个问题,我修改了精选模块中的 getProductCollection 函数,并在创建/保存集合后将以下内容添加到最后:

$_product = $this->_productCollection->getFirstItem();
$_catIDs = $_product->getCategoryIds();

if(count($_catIDs) > 0)
{
    $_cat = Mage::getModel('catalog/category')->load($_catIDs[0]); 
    Mage::register('current_category', $_cat);
}

不幸的是,仅仅在集合中的第一个项目中查看的行为会破坏工具栏中的寻呼机。无论我选择哪个分页选项,当上述代码到位时,它始终显示所有项。如果我注释掉那个部分就行了。

所以我的问题是:如何在不以某种方式更改集合或破坏分页的情况下获取有关集合中产品的任何信息?

添加我的代码以帮助更多地解释问题:

class VPS_Featured_Block_List extends Amasty_Sorting_Block_Catalog_Product_List//Mage_Catalog_Block_Product_List
{
    protected function _getProductCollection()
    {
        if (is_null($this->_productCollection))
        {
            $_attributeNames = 'featured';
            if($this->getAttributeName() != '')
            $_attributeNames = $this->getAttributeName();

            $_attrArray = explode(',', $_attributeNames);

            $this->_productCollection = Mage::getModel('catalog/product')->getCollection();

            $this->_productCollection->addAttributeToSelect('*');

            $_filters = array();

            foreach($_attrArray as $_attr)
            $_filters[] = array('attribute' => $_attr, 'eq' => true);

            $this->_productCollection->addFieldToFilter($_filters);
            //$this->_productCollection->addFieldToFilter(array(array('attribute' => $_attr, 'eq' => true),));

            Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($this->_productCollection);
            Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);
            Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_productCollection);

            //Get category of first product in collection (used for featured review)
            $_catIDs = array();

            $_product = $this->_productCollection->getFirstItem();
            $_catIDs = $_product->getCategoryIds();

            if(count($_catIDs) > 0)
            {
                $_cat = Mage::getModel('catalog/category')->load($_catIDs[0]);
                Mage::register('current_category', $_cat);
            }
        }
        return $this->_productCollection;
    }
}

2 个答案:

答案 0 :(得分:4)

感谢@clockworkgeek的回答,他发布在我的followup question

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

$secondCollection = clone $firstCollection;
$secondCollection->clear();
$_foo123 = $secondCollection->getFirstItem();

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

答案 1 :(得分:1)

很难说没有更多背景(magento版本,你所在的课程等)会发生什么。但是,我认为(60%的信心)它正在获得对产品系列的引用< strong>之前已添加其过滤器。 Magento集合延迟加载,这意味着在您明确调用load 尝试访问项目之前,不会运行数据库查询。我的猜测(再次猜测)是当你访问上面的项目时,集合加载(没有过滤器)。然后,系统的其他部分添加过滤器,但它们被忽略,因为集合已经加载

如果没有更多的背景,回答你的大问题是不可能的。