【问题标题】:Magento update product attribute without trigger reindexMagento 更新产品属性而不触发重新索引
【发布时间】:2012-05-02 01:58:34
【问题描述】:

是否可以在不触发重新索引的情况下更新产品属性?

众所周知,load()/setData()/save() 场景会触发重新索引。

我研究了“Mage_Catalog_Model_Product_Action”中的“updateAttributes”方法,但我认为它也会触发产品重新索引。

/**
     * Update attribute values for entity list per store
     *
     * @param array $productIds
     * @param array $attrData
     * @param int $storeId
     * @return Mage_Catalog_Model_Product_Action
     */
    public function updateAttributes($productIds, $attrData, $storeId)
    {
        $this->_getResource()->updateAttributes($productIds, $attrData, $storeId);
        $this->setData(array(
            'product_ids'       => array_unique($productIds),
            'attributes_data'   => $attrData,
            'store_id'          => $storeId
        ));

        // register mass action indexer event
        Mage::getSingleton('index/indexer')->processEntityAction(
            $this, Mage_Catalog_Model_Product::ENTITY, Mage_Index_Model_Event::TYPE_MASS_ACTION
        );
        return $this;
    }

'// register mass action indexer event'下的代码似乎触发了索引器操作。

【问题讨论】:

    标签: magento


    【解决方案1】:

    我找到了在不触发重新索引的情况下更新产品属性的方法。

    主要思想如下:如果你对产品的Model进行更新操作,就会触发reindex,但是如果你对Resource进行这些操作,那么具体的操作是在没有reindex的情况下完成的。

    例如:

    /**
    * This method updates product attributes then triggers reindex
    */
    Mage_Catalog_Model_Product_Action->updateAttributes($productIds, $attrData, $storeId);
    
    /**
    * This method updates product attributes but doesn't trigger reindex
    */
    Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Action->updateAttributes($productIds, $attrData, $storeId);
    

    在哪里:

    • $productIds - 包含产品 ID 的数组
    • $attrData - 一个以键为属性名,以值为属性值的数组
    • $storeId - 产品指定商店的商店ID(如果您的产品被分配到不同商店的多个类别);

    如果属性是全局的Mage_Core_Model_App::ADMIN_STORE_ID可以使用

    干杯!

    【讨论】:

    • 我不太了解您的解决方案,If the attribute is global Mage_Core_Model_App::ADMIN_STORE_ID can be used。我们如何使用它?
    • @Vdt 您可以在 $storeId 参数中使用该常量
    【解决方案2】:

    你可以先这样停止索引器:

    $processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
    $processes->walk('setMode', array(Mage_Index_Model_Process::MODE_MANUAL));
    $processes->walk('save');
    

    然后进行更新,然后像这样重新索引:

    $processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
    $processes->walk('reindexAll');
    $processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME));
    $processes->walk('save');
    

    来源:https://gist.github.com/2217520

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多