20220416035125_add_data_to_train_lass.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. use think\migration\Migrator;
  3. use think\migration\db\Column;
  4. use app\model\TrainClass;
  5. use app\model\Employee;
  6. class AddDataToTrainLass extends Migrator
  7. {
  8. /**
  9. * Change Method.
  10. *
  11. * Write your reversible migrations using this method.
  12. *
  13. * More information on writing migrations is available here:
  14. * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
  15. *
  16. * The following commands can be used in this method and Phinx will
  17. * automatically reverse them when rolling back:
  18. *
  19. * createTable
  20. * renameTable
  21. * addColumn
  22. * renameColumn
  23. * addIndex
  24. * addForeignKey
  25. *
  26. * Remember to call "create()" or "update()" and NOT "save()" when working
  27. * with the Table class.
  28. */
  29. public function change()
  30. {
  31. $train = new TrainClass();
  32. $employee = new Employee();
  33. $data = $train->where('org_id>0 and train_employee=0')->field('id,org_id,train_employee')->select()->toArray();
  34. foreach ($data as $k => $v) {
  35. $w = [];
  36. $w[] = ['org_id','in',explode(',',$v['org_id'])];
  37. $w[] = ['state','=','在职'];
  38. $eids = $employee->where($w)->column('id');
  39. $train->where([['id','=',$v['id']]])->update(['train_employee'=>implode(',',$eids)]);
  40. }
  41. }
  42. }