【问题标题】:Magento catalog category flat index refresh Enterprise 1.13+Magento 目录分类扁平化索引刷新 Enterprise 1.13+
【发布时间】:2014-05-23 11:26:26
【问题描述】:

不是一个问题,而是一个信息。

数据韭菜发生在平面表行的类别刷新上。

Bug重现步骤:

  • 创建超过 1 个类别
  • 将“默认产品列表排序依据”等属性设置为其中一个类别的非默认值并保存
  • 完全重新索引
  • 手动设置索引
  • 重新排列类别并运行企业 reindex all cronjob

在平面表中检查并查看该属性被复制到所有在包含该属性的类别之后具有位置的类别。

数据韭菜的发生是因为类别的加载是在没有重置之前加载的值的情况下完成的。

在文件中: Enterprise\Catalog\Model\Index\Action\Category\Flat\Refresh.php

搜索 _reindex 函数(第 828 行)

你会看到$category = Mage::getModel('catalog/category');正在加载的模型

修复方法是在加载新类别之前在 foreach ($categoriesIdsChunk as $categoryId) { 的每个循环上重置 $category 变量。

简单修复:

foreach ($categoriesIdsChunk as $categoryId) {
    if (!isset($attributesData[$categoryId])) {
        continue;
    }
    //add this line to reset the category data.
    $category = Mage::getModel('catalog/category');

    if ($category->load($categoryId)->getId()) {
        $data[] = $this->_prepareValuesToInsert(
            array_merge(
                $category->getData(),
                $attributesData[$categoryId],
                array('store_id' => $store->getId())
            )
        );
    }
 }       

【问题讨论】:

  • 这个错误也存在于 EE 1.14.1 中。感谢 Alex 的修复。
  • 只是对解决此问题的评论。您可能会发现自己有点摸不着头脑,因为如果您进行重写以处理上述修复,它将无法正常工作。您还需要重写 Enterprise\Catalog\Model\Index\Action\Category\Flat\Refresh\Row.php 和 Enterprise\Catalog\Model\Index\Action\Category\Flat\Refresh\Changelog.php,因为它们扩展 Enterprise\Catalog \Model\Index\Action\Category\Flat\Refresh.php.
  • “数据韭菜”听起来很好吃

标签: magento indexing refresh categories flat


【解决方案1】:

因此,我将对此主题进行详细说明,因为它很重要,但原始主题/答案有点不完整(截至 Enterprise 1.14.1)。

简单地重写a/c/c/E/Catalog/Model/Index/Action/Category/Flat/Refresh.php 只是解决方案的一半。在类别的拖放重新排序期间,在观察类别保存和移动事件的观察者中会调用以下代码...

public function processCategorySaveEvent(Varien_Event_Observer $observer)
{
    if ($this->_isLiveCategoryReindexEnabled()) {
        // ...
        $client->execute('enterprise_catalog/index_action_category_flat_refresh_row', $arguments);
    }
}

public function processCategoryMoveEvent(Varien_Event_Observer $observer)
{
    if ($this->_isLiveCategoryReindexEnabled()) {
        // ...
        $client->execute('enterprise_catalog/index_action_category_flat_refresh_changelog');
    }
};

很遗憾,enterprise_catalog/index_action_category_flat_refresh_rowenterprise_catalog/index_action_category_flat_refresh_changelog 直接扩展了 a/c/c/E/Catalog/Model/Index/Action/Category/Flat/Refresh.php,因此也需要重写。

最后,最终修复看起来更像这样......

a/c/local/Namespace/Modulename/etc/config.xml

<config>
    <global>
        <models>
            <modulename>
                <class>Namespace_Modulename_Model</class>
            </modulename>
            <!-- Enterprise_Catalog_Model_Index_Action_Category_Flat_Refresh_Row -->
            <!-- Enterprise_Catalog_Model_Index_Action_Category_Flat_Refresh_Changelog -->
            <!-- Enterprise_Catalog_Model_Index_Action_Category_Flat_Refresh -->
            <enterprise_catalog>
                <rewrite>
                    <index_action_category_flat_refresh_row>Namespace_Modulename_Model_Catalog_Index_Action_Category_Flat_Refresh_Row</index_action_category_flat_refresh_row>
                    <index_action_category_flat_refresh_changelog>Namespace_Modulename_Model_Catalog_Index_Action_Category_Flat_Refresh_Changelog</index_action_category_flat_refresh_changelog>
                    <index_action_category_flat_refresh>Namespace_Modulename_Model_Catalog_Index_Action_Category_Flat_Refresh</index_action_category_flat_refresh>
                </rewrite>
            </enterprise_catalog>
        </models>
    </global>
</config>

a/c/local/Namespace/Modulename/Model/Catalog/Index/Action/Category/Flat/Refresh/Row.php

class Namespace_Module_Model_Catalog_Index_Action_Category_Flat_Refresh_Row extends Namespace_Module_Model_Catalog_Index_Action_Category_Flat_Refresh
{
    protected $_keyColumnIdValue;

    public function __construct(array $args)
    {
        parent::__construct($args);
        if (isset($args['value'])) {
            $this->_keyColumnIdValue = $args['value'];
        }
    }

    public function execute()
    {
        if (!$this->_isFlatIndexerEnabled()) {
            return $this;
        }
        $this->_validate();
        $this->_reindex(array($this->_keyColumnIdValue));
        return $this;
    }
}

a/c/local/Namespace/Modulename/Model/Catalog/Index/Action/Category/Flat/Refresh/Changelog.php

class Namespace_Module_Model_Catalog_Index_Action_Category_Flat_Refresh_CHangelog extends Namespace_Module_Model_Catalog_Index_Action_Category_Flat_Refresh
{
    public function execute()
    {
        if (!$this->_isFlatIndexerEnabled()) {
            return $this;
        }
        $this->_validate();
        $changedIds = $this->_selectChangedIds();
        if (is_array($changedIds) && count($changedIds) > 0) {
            $idsBatches = array_chunk($changedIds, Mage::helper('enterprise_index')->getBatchSize());
            foreach ($idsBatches as $changedIds) {
                $this->_reindex($changedIds);
            }
            $this->_setChangelogValid();
        }
        return $this;
    }
}

a/c/local/Namespace/Modulename/Model/Catalog/Index/Action/Category/Flat/Refresh.php

foreach ($categoriesIdsChunk as $categoryId) {
    if (!isset($attributesData[$categoryId])) {
        continue;
    }

    // Flat Reindexing Fix
    // Flat Reindexing Fix
    $category = Mage::getModel('catalog/category');
    // Flat Reindexing Fix
    // Flat Reindexing Fix

    if ($category->load($categoryId)->getId()) {
        $data[] = $this->_prepareValuesToInsert(
            array_merge(
                $category->getData(),
                $attributesData[$categoryId],
                array('store_id' => $store->getId())
            )
        );
    }
}

我觉得可能有更好的方法来实现这一点,这种方法侵入性较小,但确实有效。请在更新时注意此代码,因为它可能需要注意。

【讨论】:

    猜你喜欢
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    相关资源
    最近更新 更多