Magento 集合第 22 条军规

标签 magento

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

正如古老的格言所说,如果不以某种方式改变实验,你就无法观察它。这似乎适用于 Magento 集合。我编写了一个运行良好的特色产品模块。我们最近在我们的商店中添加了客户评论。查看类别页面时,它会显示该类别中产品的随机评论。这也很有效。我将评论 block 添加到我的特色产品页面,这很容易做到,但由于这些产品不属于特定类别,所以它只是提取随机的、通常不相关的评论。为了解决这个问题,我修改了 Featured 模块中的 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;
    }
}

最佳答案

感谢 @clockworkgeek 在我的 followup question 中发布的这个答案:

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

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

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

关于Magento 集合第 22 条军规,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6311646/

相关文章:

Magento - 如何获取 optionsarray 中所有允许国家/地区的列表?

css - Magento CSS 属性背景图像 :url issue with data:image/svg+xml;base64

php - PayPal 网关已拒绝请求。安全 header 无效 (#10002 : Security error Magento

mysql - 从子查询插入

mysql - Magento "General error: 1005 Can' t 创建表"

php - 在 PHP Core 中将现有字符串日期转换为 'm/j/Y'

php - 确定是否启用了 Magento 模块

database - Magento - 一个模型可以有多个表吗?

magento - 在 Magento Enterprise 中设置和获取自定义客户属性

mysql - Magento 1.8 : Lock wait timeout issues when customer is checking out