M240809143855DeleteRegion.php 942 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace app\migrations;
  3. use yii\db\Migration;
  4. /**
  5. * 删除杭州下江干区-下城区
  6. * Class M240809143855DeleteRegion
  7. */
  8. class M240809143855DeleteRegion extends Migration
  9. {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function safeUp()
  14. {
  15. try {
  16. $sql = "select * from qimall_region where name in ('江干区', '下城区') and parent_id = 922 and level = 3";
  17. $res = $this->db->createCommand($sql)->queryAll();
  18. if($res){// 数据存在则删除这两个区域
  19. foreach ($res as $item){
  20. $sql = "delete from qimall_region where id = {$item['id']}";
  21. $this->execute($sql);
  22. }
  23. }
  24. } catch (\Exception $e) {
  25. }
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function safeDown()
  31. {
  32. echo "M240809143855DeleteRegion cannot be reverted.\n";
  33. return false;
  34. }
  35. }