app/DoctrineMigrations/Version20250801044652.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. /**
  7.  * Auto-generated Migration: Please modify to your needs!
  8.  */
  9. final class Version20250801044652 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'Insert categories into dtb_category with full fields and upsert';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // Tạo category cha "分類" với id = 999
  18.         $this->addSql('
  19.             INSERT INTO dtb_category
  20.                 (id, parent_category_id, creator_id, category_name, hierarchy, sort_no, create_date, update_date, discriminator_type)
  21.             VALUES
  22.                 (999, NULL, NULL, \'分類\', 1, 0, NOW(), NOW(), \'category\')
  23.             ON CONFLICT (id) DO UPDATE SET
  24.                 category_name = EXCLUDED.category_name,
  25.                 update_date = NOW()
  26.         ');
  27.         // Tạo các category con với parent_category_id = 999
  28.         $categories = [
  29.             ['id' => 1000'category_name' => '[IMEI番号]'],
  30.             ['id' => 1001'category_name' => '端末'],
  31.             ['id' => 1002'category_name' => 'ギガ追加'],
  32.             ['id' => 1003'category_name' => '海外向けギガ追加'],
  33.         ];
  34.         foreach ($categories as $cat) {
  35.             $this->addSql('
  36.                 INSERT INTO dtb_category
  37.                     (id, parent_category_id, creator_id, category_name, hierarchy, sort_no, create_date, update_date, discriminator_type)
  38.                 VALUES
  39.                     (:id, 999, NULL, :category_name, 2, 0, NOW(), NOW(), \'category\')
  40.                 ON CONFLICT (id) DO UPDATE SET
  41.                     parent_category_id = 999,
  42.                     category_name = EXCLUDED.category_name,
  43.                     hierarchy = 2,
  44.                     update_date = NOW()
  45.             ', [
  46.                 'id' => $cat['id'],
  47.                 'category_name' => $cat['category_name'],
  48.             ]);
  49.         }
  50.     }
  51.     public function down(Schema $schema): void
  52.     {
  53.         // Xóa các category con trước
  54.         $categoryIds = [1000100110021003];
  55.         $this->addSql('DELETE FROM dtb_category WHERE id IN (' implode(','$categoryIds) . ')');
  56.         // Xóa category cha
  57.         $this->addSql('DELETE FROM dtb_category WHERE id = 999');
  58.     }
  59. }