app/DoctrineMigrations/Version20260119000001.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. /**
  7.  * 商品マスタのWYSIWYG設定を更新
  8.  * selector='textarea#wysiwyg-area' を 'textarea.wysiwyg-area' に変更
  9.  */
  10. final class Version20260119000001 extends AbstractMigration
  11. {
  12.     public function up(Schema $schema): void
  13.     {
  14.         // plg_wysiwyg_editor_settingsテーブルが存在するか確認
  15.         $tableExists $this->connection->fetchOne(
  16.             "SELECT COUNT(*) FROM information_schema.TABLES 
  17.              WHERE TABLE_SCHEMA = DATABASE() 
  18.              AND TABLE_NAME = 'plg_wysiwyg_editor_settings'"
  19.         );
  20.         
  21.         if ($tableExists == 0) {
  22.             // テーブルが存在しない場合は処理をスキップ
  23.             return;
  24.         }
  25.         
  26.         // url_path='product/product/' and selector='textarea#wysiwyg-area' のレコードが存在するか確認
  27.         $oldRecordExists $this->connection->fetchOne(
  28.             "SELECT COUNT(*) FROM plg_wysiwyg_editor_settings 
  29.              WHERE url_path = 'product/product/' AND selector = 'textarea#wysiwyg-area'"
  30.         );
  31.         
  32.         if ($oldRecordExists 0) {
  33.             // 既存のレコードのselectorを更新
  34.             $this->addSql(
  35.                 "UPDATE plg_wysiwyg_editor_settings 
  36.                  SET selector = 'textarea.wysiwyg-area' 
  37.                  WHERE url_path = 'product/product/' AND selector = 'textarea#wysiwyg-area'"
  38.             );
  39.         } else {
  40.             // url_path='product/product/' and selector='textarea.wysiwyg-area' のレコードが存在するか確認
  41.             $newRecordExists $this->connection->fetchOne(
  42.                 "SELECT COUNT(*) FROM plg_wysiwyg_editor_settings 
  43.                  WHERE url_path = 'product/product/' AND selector = 'textarea.wysiwyg-area'"
  44.             );
  45.             
  46.             if ($newRecordExists == 0) {
  47.                 // レコードが存在しない場合は追加
  48.                 $maxId $this->connection->fetchOne(
  49.                     "SELECT COALESCE(MAX(id), 0) FROM plg_wysiwyg_editor_settings"
  50.                 );
  51.                 
  52.                 $nextId $maxId 1;
  53.                 
  54.                 $this->addSql(
  55.                     "INSERT INTO plg_wysiwyg_editor_settings (id, url_path, selector) VALUES (?, ?, ?)",
  56.                     [$nextId'product/product/''textarea.wysiwyg-area']
  57.                 );
  58.             }
  59.         }
  60.     }
  61.     public function down(Schema $schema): void
  62.     {
  63.         // ロールバック処理: selector を元に戻す
  64.         $this->addSql(
  65.             "UPDATE plg_wysiwyg_editor_settings 
  66.              SET selector = 'textarea#wysiwyg-area' 
  67.              WHERE url_path = 'product/product/' AND selector = 'textarea.wysiwyg-area'"
  68.         );
  69.     }
  70. }