app/DoctrineMigrations/Version20260116000000.php line 1

Open in your IDE?
  1. <?php
    
    declare(strict_types=1);
    
    namespace DoctrineMigrations;
    
    use Doctrine\DBAL\Schema\Schema;
    use Doctrine\Migrations\AbstractMigration;
    
    /**
     * セット商品テーブルに商品注意書きフィールドを追加
     * plg_wysiwyg_editor_settingsにセット商品のWYSIWYG設定を追加
     */
    final class Version20260116000000 extends AbstractMigration
    {
        public function up(Schema $schema): void
        {
            // dtb_set_product テーブルの存在確認
            if ($schema->hasTable('dtb_set_product')) {
                $table = $schema->getTable('dtb_set_product');
    
                // セット商品テーブルに商品注意書きフィールドを追加
                if (!$table->hasColumn('product_caution_note')) {
                    $this->addSql('ALTER TABLE dtb_set_product ADD product_caution_note TEXT DEFAULT NULL COMMENT \'商品注意書き\'');
                }
            }
    
            // plg_wysiwyg_editor_settingsテーブルが存在するか確認
            $tableExists = $this->connection->fetchOne(
                "SELECT COUNT(*) FROM information_schema.TABLES 
                 WHERE TABLE_SCHEMA = DATABASE() 
                 AND TABLE_NAME = 'plg_wysiwyg_editor_settings'"
            );
            
            if ($tableExists == 0) {
                // テーブルが存在しない場合は処理をスキップ
                return;
            }
            
            // url_path='product/set_product/' and selector='textarea.wysiwyg-area' の組み合わせが存在するか確認
            $recordExists = $this->connection->fetchOne(
                "SELECT COUNT(*) FROM plg_wysiwyg_editor_settings 
                 WHERE url_path = 'product/set_product/' AND selector = 'textarea.wysiwyg-area'"
            );
            
            if ($recordExists == 0) {
                // 現在の最大IDを取得
                $maxId = $this->connection->fetchOne(
                    "SELECT COALESCE(MAX(id), 0) FROM plg_wysiwyg_editor_settings"
                );
                
                $nextId = $maxId + 1;
                
                // レコードを追加
                $this->addSql(
                    "INSERT INTO plg_wysiwyg_editor_settings (id, url_path, selector) VALUES (?, ?, ?)",
                    [$nextId, 'product/set_product/', 'textarea.wysiwyg-area']
                );
            }
        }
    
        public function down(Schema $schema): void
        {
            // ロールバック処理: dtb_set_product から削除
            if ($schema->hasTable('dtb_set_product')) {
                $table = $schema->getTable('dtb_set_product');
    
                if ($table->hasColumn('product_caution_note')) {
                    $this->addSql('ALTER TABLE dtb_set_product DROP product_caution_note');
                }
            }
    
            // url_path='product/set_product/' and selector='textarea.wysiwyg-area' のレコードを削除
            $this->addSql(
                "DELETE FROM plg_wysiwyg_editor_settings 
                 WHERE url_path = 'product/set_product/' AND selector = 'textarea.wysiwyg-area'"
            );
        }
    }