app/DoctrineMigrations/Version20250828020229.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 Version20250828020229 extends AbstractMigration
  11. {
  12.     public function getDescription(): string
  13.     {
  14.         return 'Add ClassName record for Country (国)';
  15.     }
  16.     public function up(Schema $schema): void
  17.     {
  18.         $countryId ClassName::$COUNTRY_ID;
  19.         $existingRecord $this->connection->fetchOne('SELECT id FROM dtb_class_name WHERE id = ?', [$countryId]);
  20.         
  21.         if ($existingRecord) {
  22.             // Nếu đã tồn tại thì không làm gì cả
  23.             $this->write('ClassName record with ID ' $countryId ' already exists. Skipping insertion.');
  24.             return;
  25.         }
  26.         
  27.         // Lấy sort_no max hiện tại và cộng thêm 1
  28.         $maxSortNo $this->connection->fetchOne('SELECT COALESCE(MAX(sort_no), 0) FROM dtb_class_name');
  29.         $newSortNo $maxSortNo 1;
  30.         
  31.         // Lấy thời gian hiện tại
  32.         $currentDateTime date('Y-m-d H:i:s');
  33.         
  34.         // Thêm dữ liệu cho ClassName
  35.         $this->addSql('INSERT INTO dtb_class_name (id, backend_name, name, sort_no, create_date, update_date, creator_id, discriminator_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', [
  36.             $countryId,                // id
  37.             'System Class: Country',                 // backend_name
  38.             '国',                       // name
  39.             $newSortNo,                // sort_no
  40.             $currentDateTime,          // create_date
  41.             $currentDateTime,          // update_date
  42.             1,                         // creator_id
  43.             'classname'                // discriminator_type
  44.         ]);
  45.         
  46.         $this->write('Successfully added ClassName record with ID ' $countryId ' for Country (国)');
  47.     }
  48.     public function down(Schema $schema): void
  49.     {
  50.         // Sử dụng COUNTRY_ID từ ClassName entity
  51.         $countryId ClassName::$COUNTRY_ID;
  52.         
  53.         // Kiểm tra xem record có tồn tại không trước khi xóa
  54.         $existingRecord $this->connection->fetchOne('SELECT id FROM dtb_class_name WHERE id = ?', [$countryId]);
  55.         
  56.         if (!$existingRecord) {
  57.             // Nếu không tồn tại thì không làm gì cả
  58.             $this->write('ClassName record with ID ' $countryId ' does not exist. Skipping deletion.');
  59.             return;
  60.         }
  61.         
  62.         // Xóa dữ liệu đã thêm
  63.         $this->addSql('DELETE FROM dtb_class_name WHERE id = ?', [$countryId]);
  64.         
  65.         $this->write('Successfully removed ClassName record with ID ' $countryId ' for Country (国)');
  66.     }
  67. }