まったりのんびり、@vVv_kenshi_vVvです!
EC-CUBE4の開発でよく使用するので、記事にしています。
migrationコマンド一覧
ついつい調べてしまう、忘れっぽいあなたに捧げる記事です。
先頭の「php」は、サーバーの設定によってログインユーザーでは権限で弾かれる可能性があるので「php」から実行しています。
全ての実行されていないマイグレーション実行
php bin/console doctrine:migrations:migrate
マイグレーションファイルを指定して実行
php bin/console doctrine:migrations:execute --down 2021XXXXXXXXXX php bin/console doctrine:migrations:execute --up 2021XXXXXXXXXX
マイグレーションの状態確認
php bin/console doctrine:migrations:status
マイグレーションファイルを作成
php bin/console doctrine:migrations:generate
マイグレーション履歴の削除
php bin/console doctrine:migrations:version 2021XXXXXXXXXX --delete
マイグレーションファイル
コマンドで作成したマイグレーションファイル
<?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 Version20220105152818 extends AbstractMigration
{
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
}
}
定数やDBからデータ取得も可能
サンプルコード:dtb_csvへ項目追加
final class Version2022XXXXXXXXXX extends AbstractMigration
{
const ENTITY_NAME = 'Eccube\\\Entity\\Product';
const FIELD_NAME = 'new_column';
public function up(Schema $schema) : void
{
$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 ('1', null, 'Eccube\\\Entity\\Product', 'new_column', 'new_column', '新規項目', '10', '1', '2022-01-05 00:00:01', '2022-01-05 00:00:01', 'csv');");
}
public function down(Schema $schema) : void
{
$_ = function($s){return $s;};
$targetId = $this->connection->fetchColumn("SELECT id FROM dtb_csv WHERE entity_name = '{$_(self::ENTITY_NAME)}' AND field_name = '{$_(self::FIELD_NAME)}'");
if (!empty($targetId)) {
$this->addSql("DELETE FROM dtb_csv WHERE id = $targetId;");
}
}
}
配列も取得可能
$targetId = $this->connection->fetchColumn(・・・);
↓
$targetId = $this->connection->fetchArray(・・・);