<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240514053920 extends AbstractMigration
{
const csvs = [
[
'csv_type_id' => 1,
'entity_name' => 'Eccube\\\Entity\\\Product',
'field_name' => 'ProductMovies',
'reference_field_name' => 'url',
'disp_name' => 'ムービー&ギャラリー動画URL',
'enabled' => true,
],
];
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
foreach (self::csvs as $csv) {
$referece_field_name = $csv['reference_field_name'] ?? null;
$this->addSql(
"INSERT INTO dtb_csv (csv_type_id, creator_id, entity_name, field_name, reference_field_name, disp_name, sort_no, enabled, create_date, update_date, discriminator_type) VALUES (
{$csv['csv_type_id']},
null,
'{$csv['entity_name']}',
'{$csv['field_name']}',
'{$referece_field_name}',
'{$csv['disp_name']}',
(SELECT c.sort_no FROM dtb_csv as c WHERE c.csv_type_id = {$csv['csv_type_id']} ORDER BY c.sort_no DESC LIMIT 1) + 1,
1,
NOW(),
NOW(),
'csv'
)"
);
}
// ムービー&ギャラリー動画URLのsort_noを、ムービー&ギャラリー商品画像のsort_no + 1に設定したい
// ムービー&ギャラリー商品画像のsort_noを取得
$sort_no = $this->connection->fetchOne("SELECT sort_no FROM dtb_csv WHERE csv_type_id = 1 AND disp_name = 'ムービー&ギャラリー商品画像'") + 1;
// 現時点でムービー&ギャラリー商品画像のsort_no + 1以降のsort_noのレコードをさらに+1する
$this->addSql("UPDATE dtb_csv SET sort_no = sort_no + 1 WHERE csv_type_id = 1 AND sort_no >= {$sort_no}");
// ムービー&ギャラリー動画URLのsort_noを設定
$this->addSql("UPDATE dtb_csv SET sort_no = {$sort_no} WHERE csv_type_id = 1 AND field_name = 'ProductMovies'");
}
public function down(Schema $schema): void
{
foreach (self::csvs as $csv) {
$this->addSql(
"DELETE FROM dtb_csv WHERE entity_name = '{$csv['entity_name']}' AND field_name = '{$csv['field_name']}'"
);
}
}
}