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

  • 0
    The bottom line is that in the migration you need to iterate over all the lines and change one field in each construction site. But I can't find something as they move in migrations to make such a massive update
    Yii Isabelle Morgan, Feb 13, 2019

  • 1 Answers
  • 0
    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();
    }
    }
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!