M240806032700UpdateQimallMenu.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace app\migrations;
  3. use yii\db\Migration;
  4. /**
  5. * Class M240806032700UpdateQimallMenu
  6. */
  7. class M240806032700UpdateQimallMenu extends Migration
  8. {
  9. /**
  10. * {@inheritdoc}
  11. */
  12. public function safeUp()
  13. {
  14. // 先验证 qimall_menu 和 title 字段是否存在
  15. $checkSql = "
  16. SELECT
  17. (SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'qimall_menu') AS table_exists,
  18. (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'qimall_menu' AND column_name = 'title') AS column_exists
  19. ";
  20. try {
  21. $result = $this->db->createCommand($checkSql)->queryOne();
  22. if ($result['table_exists'] > 0 && $result['column_exists'] > 0) {
  23. $sql = "DELETE FROM `qimall_menu` WHERE `title` = '打印日志';";
  24. $this->execute($sql);
  25. } else {
  26. echo "Table or column does not exist.\n";
  27. }
  28. } catch (\Exception $e) {
  29. echo "Failed to delete record: " . $e->getMessage() . "\n";
  30. return true;
  31. }
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function safeDown()
  37. {
  38. echo "M240806032700UpdateQimallMenu cannot be reverted.\n";
  39. return false;
  40. }
  41. }