Reputation: 73
Just been tasked with updating around 3k SKU's. 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->setSku($new_sku);
// $product->getResource()->saveAttribute($product, 'Sku');
//
// $product->save();
}
echo "<br />DONE<br />";
}
The problem is, and as I have read the ->save() function takes around 3 seconds a product, I have also tried the ->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 explain what im doing wrong?
Upvotes: 3
Views: 2789
Reputation: 2151
You can use the following, where $productPids
is an array of product IDs.
Mage::getSingleton('catalog/product_action')
->updateAttributes($productPids, array('sku' => 'my_sku'), 0);
But in your scenario, you would have to have an array of 1 product, as this is designed for mass update of the same value, not mass update of different values.
In your case, I would be inclined (although not advocated by the community) to make the change with an SQL script, it will take less than a second to execute.
Or, if you insist on sticking to the Mage API, then disable all the "Update on save" settings for all indexes, you'll drop $product->save()
times by a significant margin, but still nowhere close to just executing a raw SQL statement.
Upvotes: 3
Reputation: 1494
using magento model for mass update is a very slow method.
However there is a model Mage_Catalog_Model_Product_Action
, that is used for mass update in magento admin. You can find its method updateAttributes
very useful
Upvotes: 0