I want to modify collection object element value without using $collection->getData() as getData() is returning an array. But I want an object to be returned.

$collection = Mage::getModel('test/test')

$i = 0; 
foreach ($collection as $key => $value) {
    //$collection[$i]['flat_last_order_amount'] = '39.99';
    //$collection[$i]['products_sku'] = '4361-4361';

I tried with commented code(with using getData()) but its not working. Any other way to do this please.


You need to use the magic getters / setters from Varien_Object.

You can do something like this:

foreach ($collection as $object) {

Only call the save method if you need to save the changes in the database.

If you need to reuse that collection with updated values you can do the following after the loop:

| improve this answer | |
  • after foreach completed when I print $collection it gives me old values only. – Nitz Mar 23 '17 at 10:11
  • @Niteen see my update – Raphael at Digital Pianism Mar 23 '17 at 10:14
  • No Raphael, its not working. I used it after for loop is completed. – Nitz Mar 23 '17 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.