E
E
Eugene2020-11-21 17:17:39
Yii
Eugene, 2020-11-21 17:17:39

How to iterate over all rows in a table in migration?

The bottom line is that in the migration you need to go through all the lines and change one field in each line. But I can’t find something how they move in migration to make such a massive update

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Melnikov, 2020-11-22
@Kosmonavt_kosmosa

I'm not sure if this is the best option, but you can always use the ActiveRecord model for this - for example, if you need to update the 'status' field in the 'records' table:

public function safeUp()
    {
        $modelsQuery = Record::find();
        
        /** @var Record $record */
        foreach ($modelsQuery->each() as $record) {
            $record->status = 'new status';
            $record->save();
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question