2012-04-20 87 views
2

是否可以在不觸發重新索引的情況下更新產品屬性?Magento更新產品屬性,無需觸發重新索引

衆所周知,load()/ setData()/ save()方案會觸發reindex。

我查看了'Mage_Catalog_Model_Product_Action'中的'updateAttributes'方法,但我認爲它也觸發了產品reindex。

/** 
    * 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'下面的代碼似乎會觸發索引器操作。

回答

5

我找到了更新產品屬性而不觸發reindex的方法。

其主要思想如下:如果您對產品的型號進行更新操作,則會觸發reindex,但是如果您在資源上執行這些操作,則該特定操作在沒有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 - 的產品陣列IDS
  • $attrData - 具有鍵作爲屬性名和值作爲屬性值的數組
  • $storeId - 產品分配的商店的商店標識(以防您的商品分配給多個分類es在不同的商店);

如果屬性是全球Mage_Core_Model_App::ADMIN_STORE_ID可以用來

乾杯!

+0

我不太瞭解您的解決方案, '如果屬性是全局的,則可以使用Mage_Core_Model_App :: ADMIN_STORE_ID'。我們如何使用它? – nXqd 2012-10-10 09:22:09

+0

@Vtt你可以在$ storeId參數中使用該常量 – 2012-12-12 05:56:41

4

你可以先停止索引這樣的:

$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

希望這有助於!