1

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')
                ->getCollection();

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

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

1 Answer 1

3

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

You can do something like this:

foreach ($collection as $object) {
    $object->setFlatLastOrderAmount("39.99");
    $object->setProductsSku('4361-4361');
    $object->save();
}

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:

$collection->clear();
$collection->load();
3
  • after foreach completed when I print $collection it gives me old values only. Commented Mar 23, 2017 at 10:11
  • @Niteen see my update Commented Mar 23, 2017 at 10:14
  • No Raphael, its not working. I used it after for loop is completed. Commented Mar 23, 2017 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.