2012-04-20 85 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

希望这有助于!