app/DoctrineMigrations/Version25010701_AddProductImageExCsv.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. final class Version25010900_AddProductImageExCsv extends AbstractMigration
  7. {
  8.     public function getDescription(): string
  9.     {
  10.         return 'Import product image ex data from CSV';
  11.     }
  12.     private function loadCsvData(): array
  13.     {
  14.         // CSVファイルのパスを指定
  15.         $csvPath __DIR__ '/Data/senri_web_data.csv';
  16.         
  17.         if (!file_exists($csvPath)) {
  18.             throw new \RuntimeException('CSV file not found: ' $csvPath);
  19.         }
  20.         $data = [];
  21.         if (($handle fopen($csvPath'r')) !== false) {
  22.             // ヘッダー行をスキップ
  23.             fgetcsv($handle);
  24.             
  25.             while (($row fgetcsv($handle)) !== false) {
  26.                 // CSVの列順序に合わせて配列にマッピング
  27.                 $data[] = [
  28.                     'product_id' => $row[0],
  29.                     'large_image' => $row[1],
  30.                     'middle_image' => $row[2],
  31.                     'small_image' => $row[3],
  32.                     'display_description' => $row[4],
  33.                     'accessibility_text' => $row[5],
  34.                     'data_type_no' => $row[6],
  35.                 ];
  36.             }
  37.             fclose($handle);
  38.         }
  39.         
  40.         return $data;
  41.     }
  42.     public function up(Schema $schema): void
  43.     {
  44.       try {
  45.         // まずテーブルをTRUNCATE
  46.         $this->addSql('SET FOREIGN_KEY_CHECKS = 0');
  47.         $this->addSql('TRUNCATE TABLE dtb_product_image_ex');
  48.         $this->addSql('SET FOREIGN_KEY_CHECKS = 1');
  49.          
  50.         $images $this->loadCsvData();
  51.          
  52.         // 存在する商品IDを取得
  53.         $existingProductIds $this->connection->fetchFirstColumn('SELECT id FROM dtb_product');
  54.         foreach ($images as $image) {
  55.           // 商品が存在する場合のみ挿入
  56.           if (in_array($image['product_id'], $existingProductIds)) {
  57.             $this->addSql(
  58.               'INSERT INTO dtb_product_image_ex (
  59.                 product_id,
  60.                 large_image,
  61.                 middle_image,
  62.                 small_image,
  63.                 display_description,
  64.                 accessibility_text,
  65.                 data_type_no,
  66.                 create_date,
  67.                 update_date,
  68.                 discriminator_type
  69.               ) VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), NOW(), ?)',
  70.               [
  71.                 $image['product_id'],
  72.                 $image['large_image'],
  73.                 $image['middle_image'],
  74.                 $image['small_image'],
  75.                 $image['display_description'],
  76.                 $image['accessibility_text'],
  77.                 $image['data_type_no'],
  78.                 'productimageex'
  79.               ]
  80.             );
  81.           } else {
  82.             $this->write(sprintf('Skipping product_id %s: Product does not exist'$image['product_id']));
  83.           }
  84.         }
  85.       } catch (\Exception $e) {
  86.         $this->write('Migration failed: ' $e->getMessage());
  87.         throw $e;
  88.       }
  89.     }
  90.     public function down(Schema $schema): void
  91.     {
  92.         // 追加したすべてのデータを削除
  93.         $this->addSql('TRUNCATE TABLE dtb_product_image_ex');
  94.     }
  95. }