I ran into an issue when I was attempting to update every row within a collection I had previously loaded. The only method I could easily see to do this directly on the collection was via a foreach loop with a save after each setData/addData.

There are other methods throughout Magento to update individual product attributes and other data, but if you’re wanting to call a simple update function on a collection you can implement the following function into your collection resource model.

This function allows you to easily call massUpdate(array()), which in turn updates all the current loaded rows with the specified data.

For instance:

This will only work on flat tables and not any form of data which is stored within EAV.

Dave Macaulay
Dave Macaulay
I'm an enthusiastic, slightly eccentric Magento developer who bores with the idea of another basic brief.
  • Roberto Massimi

    Hi Dave,
    good! many thanks.
    but if my collection is empty, the massUpdate function fail.
    I update your function with this check:

    if (empty($this->getAllIds()))
    {
    return;
    }

    Best,
    Roberto

    • daavem

      Hey Roberto, typically I wouldn’t try to run a massUpdate call without first validating the collection had items to update. However, having an additional check internally in the function can’t hurt (apart from visibility from the original call location).