M250610104039InsertTown.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace app\migrations;
  3. use yii\db\Migration;
  4. /**
  5. * 因为区域管理表不是自增Id。所以需要指定ID 之前有错误数据需要删除
  6. * Class M250610104039InsertTown
  7. */
  8. class M250610104039InsertTown extends Migration
  9. {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function safeUp()
  14. {
  15. // 先执行之前数据的删除 在重新插入
  16. try {
  17. $sql = <<<SQL
  18. DELETE FROM `qimall_town` WHERE (`adcode` = 441303 OR `adcode` = 441302) AND `name` in ('澳头街道', '‌西区街道‌', '‌霞涌街道', '‌陈江街道', '‌惠环街道', '沥林镇', '‌潼侨镇', '‌潼湖镇');
  19. INSERT INTO `qimall_town` (`district_id`, `adcode`, `name`, `created_at`, `updated_at`) VALUES (10320, 441303, '澳头街道', 1742278841, 1742278841);
  20. INSERT INTO `qimall_town` (`district_id`, `adcode`, `name`, `created_at`, `updated_at`) VALUES (10320, 441303, '‌西区街道‌', 1742278841, 1742278841);
  21. INSERT INTO `qimall_town` (`district_id`, `adcode`, `name`, `created_at`, `updated_at`) VALUES (10320, 441303, '‌霞涌街道', 1742278841, 1742278841);
  22. INSERT INTO `qimall_town` (`district_id`, `adcode`, `name`, `created_at`, `updated_at`) VALUES (10321, 441302, '‌陈江街道', 1742278841, 1742278841);
  23. INSERT INTO `qimall_town` (`district_id`, `adcode`, `name`, `created_at`, `updated_at`) VALUES (10321, 441302, '‌惠环街道', 1742278841, 1742278841);
  24. INSERT INTO `qimall_town` (`district_id`, `adcode`, `name`, `created_at`, `updated_at`) VALUES (10321, 441302, '沥林镇', 1742278841, 1742278841);
  25. INSERT INTO `qimall_town` (`district_id`, `adcode`, `name`, `created_at`, `updated_at`) VALUES (10321, 441302, '‌潼侨镇', 1742278841, 1742278841);
  26. INSERT INTO `qimall_town` (`district_id`, `adcode`, `name`, `created_at`, `updated_at`) VALUES (10321, 441302, '‌潼湖镇', 1742278841, 1742278841);
  27. SQL;
  28. $this->execute($sql);
  29. } catch (\Exception $e) {
  30. // 处理异常
  31. var_dump($e->getMessage());
  32. }
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function safeDown()
  38. {
  39. echo "M250610104039InsertTown cannot be reverted.\n";
  40. return false;
  41. }
  42. }