Updating all table rows using Active Record

Hi,

I use the following code to update all rows in a table using Active Record:




$reader = User::model()->findAll();

foreach ($reader as $row) {

    $row->fname = 'something';

    $row->save();

}



But this updates only the last record. What is wrong?

Thank you.

Try to use




$row->save(false); 



in order to disable model validation, just to make sure that this isn’t the problem. Maybe the last record you load via findAll() is the only one that has a “required” attribute already set, all others don’t.

Just tested it with my Ar model , no problems… this should work :rolleyes:

The only reason it can fail is the validation as Haensel mentioned

Thank you!

You were right. The last record was the only valid one.