tags:

views:

1729

answers:

2

So I'm loading a collection of products using this code:

$magento_time= 'some time string';
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addFieldToFilter(array(
    array('attribute'=> 'updated_at', 'gt'=> date('Y-m-d H:i:s', strtotime($magento_time))),
));    
$collection->save();

And getting this error in return on save:

Warning: Invalid argument supplied for foreach()  in /var/www/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 970

#0 /var/www/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php(970): mageCoreErrorHandler(2, 'Invalid argumen...', '/var/www/magent...', 970, Array)
#1 /var/www/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php(925): Mage_Eav_Model_Entity_Abstract->_collectSaveData(Object(Mage_Catalog_Model_Product))
#2 /var/www/magento/app/code/core/Mage/Core/Model/Abstract.php(251): Mage_Eav_Model_Entity_Abstract->save(Object(Mage_Catalog_Model_Product))
#3 /var/www/magento/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php(845): Mage_Core_Model_Abstract->save()
#4 /var/www/magento/app/code/local/MyModule/controllers/IndexController.php(16): Mage_Eav_Model_Entity_Collection_Abstract->save()
#5 /var/www/magento/app/code/core/Mage/Core/Controller/Varien/Action.php(376): MyModule_IndexController->sayHelloAction()
#6 /var/www/magento/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(248): Mage_Core_Controller_Varien_Action->dispatch('sayHello')
#7 /var/www/magento/app/code/core/Mage/Core/Controller/Varien/Front.php(158): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#8 /var/www/magento/app/Mage.php(459): Mage_Core_Controller_Varien_Front->dispatch()
#9 /var/www/magento/index.php(68): Mage::run()
#10 {main}

How do I save products back to the database after they've been loaded into a collection?

A: 

$collection->save() works with the following changes:

  • Updated the core code so it checks for a valid array on line 970.

  • Added the following call to the controller:

    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

pygorex1
You should *never* hack the core code as it will break future upgradability. Search for tutorials on overriding controllers and then make the changes suggested
Jonathan Day
I don't call it "hacking the core" - I call it "fixing a bug". If there's a possibility that a variable will not be an array then the code should check for this state before feeding the var to `foreach`. If the Magento maintainers were a little more thorough and actually bothered to document their code I'd have little incentive to "hack" anything. For instance: I never modify the Wordpress or Drupal core and always fork plugins that need modifications. Internally, Magento is so convoluted, sometimes one is forced to hack to meet a deadline.
pygorex1
+1  A: 

You do a foreach and save elements one by one. It is not a good idea to touch the core.

Elzo Valugi