More efficient way to update an attribute on a Magento product

An update of about 3 thousand SKUs was just asked. I wrote a script that looks like this:

if($updates_handle) { 
    while($sku_entry=fgetcsv($updates_handle, 1024, ",")) { 

        /* get the old and new Product SKU values */
        list($old_sku, $new_sku) = $sku_entry;

        echo 'Setting: '.$old_sku.' SKU to: '.$new_sku.'<br />';

        $product = Mage::getModel('catalog/product')->loadByAttribute('skuref1', $old_sku);


//      $product->getResource()->saveAttribute($product, 'Sku');
//      $product->save();

    echo "<br />DONE<br />";


The problem is, as I read, the -> save () function takes about 3 seconds of the product, I also tried -> saveAttribute, but that gives me an error:

Fatal error: Uncaught exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'catalog_product_entity.value_id' in 'field list'' in /var/www/website/httpdocs/lib/Zend/Db/Statement/Pdo.php:234 Stack trace: #0 /var/www/website/httpdocs/lib/Zend/Db/Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array) #1 /var/www/website/httpdocs/lib/Zend/Db/Adapter/Abstract.php(468): Zend_Db_Statement->execute(Array) #2 /var/www/website/httpdocs/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('SELECT `catalog...', Array) #3 /var/www/website/httpdocs/lib/Varien/Db/Adapter/Pdo/Mysql.php(333): Zend_Db_Adapter_Pdo_Abstract->query('SELECT `catalog...', Array) #4 /var/www/website/httpdocs/lib/Zend/Db/Adapter/Abstract.php(799): Varien_Db_Adapter_Pdo_Mysql->query(Object(Varien_Db_Select), Array) #5 /var/www/website/httpdocs/app/code/core/Mag in /var/www/website/httpdocs/lib/Zend/Db/Statement/Pdo.php on line 234


Can someone please explain what I am doing wrong?


source to share

2 answers

You can use the following, where $productPids

is an array of product IDs.

    ->updateAttributes($productPids, array('sku' => 'my_sku'), 0);


But in your scenario, you should have an array of 1 product, as it is meant to bulk update the same value, not bulk update different values.

In your case, I would be inclined (although not protected by the community) to make changes with a SQL script takes less than a second to execute.

Or, if you insist on sticking to the Mage API, turn off all Refresh on Save settings for all indexes, you will be throwing away $product->save()

a significant amount of times, but still nowhere close to simply executing a raw SQL query.



using magento model for bulk update is a very slow method. However, there is a model Mage_Catalog_Model_Product_Action

that is used for bulk update in magento admin. You may find his method updateAttributes

very useful.



All Articles