app/DoctrineMigrations/Version20260119000001.php line 1

Open in your IDE?
  1. <?php
    
    declare(strict_types=1);
    
    namespace DoctrineMigrations;
    
    use Doctrine\DBAL\Schema\Schema;
    use Doctrine\Migrations\AbstractMigration;
    
    /**
     * 商品マスタのWYSIWYG設定を更新
     * selector='textarea#wysiwyg-area' を 'textarea.wysiwyg-area' に変更
     */
    final class Version20260119000001 extends AbstractMigration
    {
        public function up(Schema $schema): void
        {
            // 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/product/' and selector='textarea#wysiwyg-area' のレコードが存在するか確認
            $oldRecordExists = $this->connection->fetchOne(
                "SELECT COUNT(*) FROM plg_wysiwyg_editor_settings 
                 WHERE url_path = 'product/product/' AND selector = 'textarea#wysiwyg-area'"
            );
            
            if ($oldRecordExists > 0) {
                // 既存のレコードのselectorを更新
                $this->addSql(
                    "UPDATE plg_wysiwyg_editor_settings 
                     SET selector = 'textarea.wysiwyg-area' 
                     WHERE url_path = 'product/product/' AND selector = 'textarea#wysiwyg-area'"
                );
            } else {
                // url_path='product/product/' and selector='textarea.wysiwyg-area' のレコードが存在するか確認
                $newRecordExists = $this->connection->fetchOne(
                    "SELECT COUNT(*) FROM plg_wysiwyg_editor_settings 
                     WHERE url_path = 'product/product/' AND selector = 'textarea.wysiwyg-area'"
                );
                
                if ($newRecordExists == 0) {
                    // レコードが存在しない場合は追加
                    $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/product/', 'textarea.wysiwyg-area']
                    );
                }
            }
        }
    
        public function down(Schema $schema): void
        {
            // ロールバック処理: selector を元に戻す
            $this->addSql(
                "UPDATE plg_wysiwyg_editor_settings 
                 SET selector = 'textarea#wysiwyg-area' 
                 WHERE url_path = 'product/product/' AND selector = 'textarea.wysiwyg-area'"
            );
        }
    }