<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Eccube\Entity\ClassName;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250828020229 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add ClassName record for Country (国)';
}
public function up(Schema $schema): void
{
$countryId = ClassName::$COUNTRY_ID;
$existingRecord = $this->connection->fetchOne('SELECT id FROM dtb_class_name WHERE id = ?', [$countryId]);
if ($existingRecord) {
// Nếu đã tồn tại thì không làm gì cả
$this->write('ClassName record with ID ' . $countryId . ' already exists. Skipping insertion.');
return;
}
// Lấy sort_no max hiện tại và cộng thêm 1
$maxSortNo = $this->connection->fetchOne('SELECT COALESCE(MAX(sort_no), 0) FROM dtb_class_name');
$newSortNo = $maxSortNo + 1;
// Lấy thời gian hiện tại
$currentDateTime = date('Y-m-d H:i:s');
// Thêm dữ liệu cho ClassName
$this->addSql('INSERT INTO dtb_class_name (id, backend_name, name, sort_no, create_date, update_date, creator_id, discriminator_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', [
$countryId, // id
'System Class: Country', // backend_name
'国', // name
$newSortNo, // sort_no
$currentDateTime, // create_date
$currentDateTime, // update_date
1, // creator_id
'classname' // discriminator_type
]);
$this->write('Successfully added ClassName record with ID ' . $countryId . ' for Country (国)');
}
public function down(Schema $schema): void
{
// Sử dụng COUNTRY_ID từ ClassName entity
$countryId = ClassName::$COUNTRY_ID;
// Kiểm tra xem record có tồn tại không trước khi xóa
$existingRecord = $this->connection->fetchOne('SELECT id FROM dtb_class_name WHERE id = ?', [$countryId]);
if (!$existingRecord) {
// Nếu không tồn tại thì không làm gì cả
$this->write('ClassName record with ID ' . $countryId . ' does not exist. Skipping deletion.');
return;
}
// Xóa dữ liệu đã thêm
$this->addSql('DELETE FROM dtb_class_name WHERE id = ?', [$countryId]);
$this->write('Successfully removed ClassName record with ID ' . $countryId . ' for Country (国)');
}
}