20230423023915_create_building_label_table.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. use app\model\BuildingLabel;
  3. use app\model\Company;
  4. use think\migration\Migrator;
  5. use think\migration\db\Column;
  6. class CreateBuildingLabelTable 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. $this->table('building_label')
  32. ->addColumn('name', 'string', ['comment' => '标签名'])
  33. ->addColumn('root_id', 'integer', ['comment' => '企业id'])
  34. ->addColumn('addtime', 'integer', ['comment' => '添加时间'])
  35. ->create();
  36. $default = ['周边学校', '周边医院', '周边商超', '周边建材', '公园', '小区进度'];
  37. $rootList = Company::column('root_id');
  38. $data = [];
  39. foreach ($rootList as $id) {
  40. foreach ($default as $d) {
  41. $data[] = [
  42. 'name' => $d,
  43. 'root_id' => $id,
  44. 'addtime' => time()
  45. ];
  46. }
  47. }
  48. (new BuildingLabel)->saveAll($data);
  49. }
  50. }