Magento:更快地更新产品目录

The*_*yom 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();   
            }
Run Code Online (Sandbox Code Playgroud)

虽然这项工作,每个保存是一个SQL更新查询,事实是有一个非常大的目录,这是相当慢.

我想知道是否可以通过单独保存集合而不是产品来加速这一点.

Jas*_*ten 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');
Run Code Online (Sandbox Code Playgroud)

  • 当我尝试更新`category_ids`时,我在第69行的`app\code\core\Mage\Catalog\Model\Resource\Product\Action.php中的非对象上调用成员函数getAttributeId().有没有办法以这种方式修改"属性"(它不是真正的属性),或者比`$ product-> setCategoryIds('1,2,3') - > save();更快! (2认同)