Magento model collections - save data

Magento model collections - save data

July 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento model collections - save data

Solving Magento Product Collection Saving Issues

Introduction

In Magento, loading product collections is a common task for developers working on customizations or extensions. However, when it comes to saving these loaded collections back to the database, many face challenges and errors like the one described in this post.

Let’s take a closer look at the provided code, understand why the error occurs, and then provide a step-by-step solution using best practices.

The Existing Code

The given script attempts to load products with updated_at greater than a specific date and save them back to the database:

$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();

The Issue at Hand

The error occurs because the save() method on a collection doesn’t work as expected. When you call $collection->save(), Magento tries to iterate through each loaded product to collect data for saving, but it fails when there are no products in the collection (which happens if your filter conditions don’t match any records).

This is evident from the error message pointing to line 970 of Mage_Eav_Model_Entity_Abstract.php:

Warning: Invalid argument supplied for foreach()  ... Mage_Eav_Model_Entity_Abstract.php on line 970

The Solution Approach

Rather than trying to save the collection directly, we need to iterate through each product in the collection and call save() individually. This way, Magento can properly gather and process the data for each entity.

Here’s an updated version of your 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))),
));

foreach ($collection as $product) {
    try {
        $product->save();
    } catch (Exception $e) {
        Mage::logException($e);
    }
}

Best Practices and Considerations

  1. Error Handling: Always wrap your save operations within a try-catch block to handle any exceptions that may occur during the saving process.

  2. Logging: Use Mage::logException() to log any caught exceptions, ensuring you have visibility into potential issues.

  3. Performance: If dealing with large collections, consider using Magento’s built-in indexing mechanisms for updating product data instead of manually iterating through each product. This can significantly improve performance and reduce the risk of timeouts or memory limits.

You can learn more about custom indexing in our guide to Magento Indexing.

Conclusion

In this post, we identified the cause of the “Invalid argument supplied for foreach()” error when trying to save Magento product collections and provided a solution using proper iteration and exception handling.

Remember, Magento’s core functionality often requires careful management of objects and data to avoid unexpected errors. By following best practices such as individual object saving and proper error handling, you can ensure your customizations remain stable and maintainable.

Happy coding!

Catalog Database Extensions Errors

Our network