app/DoctrineMigrations/Version20250828022236.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. use Eccube\Entity\ClassName;
  7. /**
  8.  * Auto-generated Migration: Please modify to your needs!
  9.  */
  10. final class Version20250828022236 extends AbstractMigration
  11. {
  12.     public function getDescription(): string
  13.     {
  14.         return 'Add country list to ClassCategory for Country (国)';
  15.     }
  16.     public function up(Schema $schema): void
  17.     {
  18.         $countryId ClassName::$COUNTRY_ID;
  19.         
  20.         // Kiểm tra xem ClassName với id = 1000 có tồn tại không
  21.         $existingClassName $this->connection->fetchOne('SELECT id FROM dtb_class_name WHERE id = ?', [$countryId]);
  22.         
  23.         if (!$existingClassName) {
  24.             $this->write('ClassName record with ID ' $countryId ' does not exist. Skipping country categories insertion.');
  25.             return;
  26.         }
  27.         
  28.         // Danh sách các quốc gia
  29.         $countries = [
  30.             ['name' => '日本''backend_name' => 'Japan''sort_no' => 1],
  31.             ['name' => 'アメリカ''backend_name' => 'United States''sort_no' => 2],
  32.             ['name' => '中国''backend_name' => 'China''sort_no' => 3],
  33.             ['name' => '韓国''backend_name' => 'South Korea''sort_no' => 4],
  34.             ['name' => 'イギリス''backend_name' => 'United Kingdom''sort_no' => 5],
  35.             ['name' => 'フランス''backend_name' => 'France''sort_no' => 6],
  36.             ['name' => 'ドイツ''backend_name' => 'Germany''sort_no' => 7],
  37.             ['name' => 'イタリア''backend_name' => 'Italy''sort_no' => 8],
  38.             ['name' => 'スペイン''backend_name' => 'Spain''sort_no' => 9],
  39.             ['name' => 'カナダ''backend_name' => 'Canada''sort_no' => 10],
  40.             ['name' => 'オーストラリア''backend_name' => 'Australia''sort_no' => 11],
  41.             ['name' => 'ブラジル''backend_name' => 'Brazil''sort_no' => 12],
  42.             ['name' => 'インド''backend_name' => 'India''sort_no' => 13],
  43.             ['name' => 'ロシア''backend_name' => 'Russia''sort_no' => 14],
  44.             ['name' => 'ベトナム''backend_name' => 'Vietnam''sort_no' => 15],
  45.             ['name' => 'タイ''backend_name' => 'Thailand''sort_no' => 16],
  46.             ['name' => 'シンガポール''backend_name' => 'Singapore''sort_no' => 17],
  47.             ['name' => 'マレーシア''backend_name' => 'Malaysia''sort_no' => 18],
  48.             ['name' => 'インドネシア''backend_name' => 'Indonesia''sort_no' => 19],
  49.             ['name' => 'フィリピン''backend_name' => 'Philippines''sort_no' => 20],
  50.         ];
  51.         
  52.         $currentDateTime date('Y-m-d H:i:s');
  53.         $insertedCount 0;
  54.         
  55.         foreach ($countries as $country) {
  56.             // Kiểm tra xem category đã tồn tại chưa
  57.             $existingCategory $this->connection->fetchOne(
  58.                 'SELECT id FROM dtb_class_category WHERE class_name_id = ? AND name = ?'
  59.                 [$countryId$country['name']]
  60.             );
  61.             
  62.             if ($existingCategory) {
  63.                 $this->write('ClassCategory for ' $country['name'] . ' already exists. Skipping.');
  64.                 continue;
  65.             }
  66.             
  67.             // Thêm category mới
  68.             $this->addSql(
  69.                 'INSERT INTO dtb_class_category (backend_name, name, sort_no, visible, create_date, update_date, class_name_id, creator_id, discriminator_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
  70.                 [
  71.                     $country['backend_name'],    // backend_name
  72.                     $country['name'],            // name
  73.                     $country['sort_no'],         // sort_no
  74.                     1,                           // visible (true)
  75.                     $currentDateTime,            // create_date
  76.                     $currentDateTime,            // update_date
  77.                     $countryId,                  // class_name_id
  78.                     1,                           // creator_id
  79.                     'classcategory'              // discriminator_type
  80.                 ]
  81.             );
  82.             
  83.             $insertedCount++;
  84.         }
  85.         
  86.         $this->write('Successfully added ' $insertedCount ' country categories for ClassName ID ' $countryId);
  87.     }
  88.     public function down(Schema $schema): void
  89.     {
  90.         $countryId ClassName::$COUNTRY_ID;
  91.         
  92.         // Kiểm tra xem ClassName có tồn tại không
  93.         $existingClassName $this->connection->fetchOne('SELECT id FROM dtb_class_name WHERE id = ?', [$countryId]);
  94.         
  95.         if (!$existingClassName) {
  96.             $this->write('ClassName record with ID ' $countryId ' does not exist. Skipping country categories deletion.');
  97.             return;
  98.         }
  99.         
  100.         // Xóa tất cả ClassCategory thuộc về ClassName này
  101.         $deletedCount $this->connection->executeStatement(
  102.             'DELETE FROM dtb_class_category WHERE class_name_id = ?',
  103.             [$countryId]
  104.         );
  105.         
  106.         $this->write('Successfully removed ' $deletedCount ' country categories for ClassName ID ' $countryId);
  107.     }
  108. }