The essence in comparison with yii1 practically differs nothing.
You make a pattern. Then we set global configurations for the console application. When you run the migrate command, these settings will be applied without entering any additional parameters.
Example.
Create a migration pattern along the path migration/templates/mg.php Looks like this:
<?php /** * This view is used by console/controllers/MigrateController.php * The following variables are available in this view: */ /* @var $className string the new migration class name */ echo "<?php\n"; ?> use yii\db\Migration; class <?= $className ?> extends Migration { public function up() { $tableOptions = null; if ($this->db->driverName === 'mysql') { // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; } } public function down() { echo "<?= $className ?> cannot be reverted.\n"; return false; } /* // Use safeUp/safeDown to run migration code within a transaction public function safeUp() { } public function safeDown() { } */ }
then in config/console.php we write the settings:
return [ ... 'controllerMap' => [ 'migrate' => [ 'class' => 'yii\console\controllers\MigrateController', 'templateFile' => '@app/migrations/templates/mg.php', ], ], ... ];
It's enough. If you want to make your own controller, then as an example:
controller itself
<?php namespace app\components; use yii\db\Schema; use yii\db\Migration; class MGMigration extends Migration{ }
pattern, your way .. for example migrations/templates/mg.php
<?php echo "<?php\n"; ?> use yii\db\Schema; use yii\db\Migration; use app\components\MGMigration; class <?= $className ?> extends MGMigration { public function safeUp() { } public function safeDown() { } }
and again global settings in config/console.php
'controllerMap' => [ 'migrate' => [ 'class' => 'yii\console\controllers\MigrateController', 'migrationTable' => 'mg_migration', 'templateFile' => '@app/migrations/templates/mg.php' ], ],