How do I update values ​​for Magento products using install / update scripts and Magento abstractions?

I added the eav attribute for my Magento application product object using the installer script (basically following the procedure described here: Setting custom attributes using the Module ). Now I want to use a script update to change (fill) the values ​​of this attribute for each product according to some criteria (depending on the product category). I tried using a script essentially like this:

$attributeValues = array(...) // Map from $productId to the desired $value $product = Mage::getModel('catalog/product'); foreach($attributeValues as $productId=>$value){ $product->load($productId)->setMyAttribute($value); $product->save(); } 

My questions would be: Can this level of abstraction (Mage :: getModel ("catalog / product") and its methods) be used in upgrade scripts? If this is not the case, how would you recommend changing these attribute values ​​using update scripts (without sql requirement)?

script I used (so far) did not work and could not fail:

 Call to a member function getStoreIds() on a non-object 

in the core magento file.

I do not know if this error is a Magento error or a problem with the way I use update scripts.

I am using Magento 1.4.0.1

+4
source share
2 answers

Try adding Mage::app()->setUpdateMode(false) to the sql script update. eg

 $installer = new Mage_Eav_Model_Entity_Setup('core_setup');; $installer->startSetup(); Mage::app()->setUpdateMode(false); Mage::app()->setCurrentStore('your_store_code_here'); 

If you look in Mage::app()->getStore() , you will see the following snippet that returns the wrong store needed to save the product.

 if (!Mage::isInstalled() || $this->getUpdateMode()) { return $this->_getDefaultStore(); } 
+1
source

Data refresh scripts are the way to go

To do this, simply use the data update script. This is a script placed in the data folder instead of the sql folder. These scripts run later than database structure updates, and allow access to more functionality.

Examples of file names:

 app/code/local/My/Module/data/your_setup/data-install-0.1.0.php app/code/local/My/Module/data/your_setup/data-upgrade-0.1.0-0.2.0.php 

This one is already available in Magento 1.4 .

+2
source

All Articles