M250314082020AddRegion.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace app\migrations;
  3. use common\models\common\Region;
  4. use yii\db\Migration;
  5. /**
  6. * Class M250314082020AddRegion
  7. */
  8. class M250314082020AddRegion extends Migration
  9. {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function safeUp()
  14. {
  15. try {
  16. $addressA = Region::findOne(['name' => '仲恺高新技术产业开发区']);
  17. $addressAid = $addressA['id'];
  18. $addressB = Region::findOne(['name' => '大亚湾经济技术开发区']);
  19. $addressBid = $addressB['id'];
  20. $sql = <<<SQL
  21. INSERT INTO `qimall_town` (`district_id`, `adcode`, `name`, `created_at`, `updated_at`) VALUES (2025, 441303, '澳头街道', 1742278841, 1742278841);
  22. INSERT INTO `qimall_town` (`district_id`, `adcode`, `name`, `created_at`, `updated_at`) VALUES (2025, 441303, '‌西区街道‌', 1742278841, 1742278841);
  23. INSERT INTO `qimall_town` (`district_id`, `adcode`, `name`, `created_at`, `updated_at`) VALUES (2025, 441303, '‌霞涌街道', 1742278841, 1742278841);
  24. INSERT INTO `qimall_town` (`district_id`, `adcode`, `name`, `created_at`, `updated_at`) VALUES (2026, 441302, '‌陈江街道', 1742278841, 1742278841);
  25. INSERT INTO `qimall_town` (`district_id`, `adcode`, `name`, `created_at`, `updated_at`) VALUES (2026, 441302, '‌惠环街道', 1742278841, 1742278841);
  26. INSERT INTO `qimall_town` (`district_id`, `adcode`, `name`, `created_at`, `updated_at`) VALUES (2026, 441302, '沥林镇', 1742278841, 1742278841);
  27. INSERT INTO `qimall_town` (`district_id`, `adcode`, `name`, `created_at`, `updated_at`) VALUES (2026, 441302, '‌潼侨镇', 1742278841, 1742278841);
  28. INSERT INTO `qimall_town` (`district_id`, `adcode`, `name`, `created_at`, `updated_at`) VALUES (2026, 441302, '‌潼湖镇', 1742278841, 1742278841);
  29. SQL;
  30. // 将parent_id替换为$addressAid
  31. $sql = str_replace("2025", $addressBid, $sql);
  32. $sql = str_replace("2026", $addressAid, $sql);
  33. $this->execute($sql);
  34. } catch (\Exception $e) {
  35. // 处理异常
  36. var_dump($e->getMessage());
  37. }
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function safeDown()
  43. {
  44. echo "M250314082020AddRegion cannot be reverted.\n";
  45. return false;
  46. }
  47. }