2011-11-17 94 views
7

我寫了很多腳本來更新我的產品目錄根據一些或其他參數。在他們每個人的基本邏輯是什麼呈三角這...Magento:更新產品目錄更快

 //Get collection 
    $collection = Mage::getModel('catalog/product')->getCollection(); 
    $collection->addAttributeToSelect('sku'); 
$collection->addAttributeToSelect('publihser'); 
    $collection->addFieldToFilter(array(array('attribute'=>'publisher','eq'=>$publisher))); 

    // for each product in collection do a individual save 
    foreach ($collection as $product) { 
    $product->setSKU($newValue); 
    $product->save(); 
      } 

雖然這項工作,每個省是一個SQL更新查詢的事實是,有一個非常大的目錄,這是相當緩慢的。

我想知道是否可以通過在產品上進行單個保存而不是在集合上進行加速。

+0

這是否在瀏覽器或cli中運行? – djdy

+2

嘗試[MAGMI](http://sourceforge.net/apps/mediawiki/magmi/index.php?title=Magmi_Wiki)。 – 2011-11-17 12:44:41

+0

@djdy這是在CLI上運行的,但也可以在Web界面上運行。 – TheVyom

回答

21

您可以通過幾件事情來編寫更快的更新腳本。我不知道你是如何得到你的一些變量,所以你需要修改它以適應你的情況,但是下面的代碼應該比你現在做的要快得多。例如:

// Set indexing to manual before starting updates, otherwise it'll continually get slower as you update 
$processes = Mage::getSingleton('index/indexer')->getProcessesCollection(); 
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_MANUAL)); 
$processes->walk('save'); 

// Get Collection 
$collection = Mage::getModel('catalog/product')->getCollection() 
    ->addAttributeToSelect('sku') 
    ->addAttributeToSelect('publihser') 
    ->addFieldToFilter(array(array('attribute'=>'publisher','eq'=>$publisher))); 


function productUpdateCallback($args){ 
    $product = Mage::getModel('catalog/product'); 

    $product->setData($args['row']); 

    $productId = $product->getId(); 

    $sku = 'yourSku'; 

    // Updates a single attribute, much faster than calling a full product save 
    Mage::getSingleton('catalog/product_action') 
     ->updateAttributes(array($productId), array('sku' => $sku), 0); 
} 

// Walk through collection, for large collections this is much faster than using foreach 
Mage::getSingleton('core/resource_iterator')->walk($collection->getSelect(), array('productUpdateCallback')); 


// Reindex all 
$processes->walk('reindexAll'); 
// Set indexing back to realtime, if you have it set to manual normally you can comment this line out 
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME)); 
$processes->walk('save'); 
+0

謝謝@Jasuten。你的答案充滿了我可以用來提高速度的想法。 – TheVyom

+2

當我嘗試更新'category_ids'時,我得到了'在第69行的\ app \ code \ core \ Mage \ Catalog \ Model \ Resource \ Product \ Action.php中的非對象上調用成員函數getAttributeId()。有沒有辦法修改這個「屬性」(這不是一個真正的屬性),或者比'$ product-> setCategoryIds('1,2,3') - > save();' –