I'm trying to change the template for migration to yii2. To do this, override MigrateController inside the console / controllers folder like this:

namespace console\controllers; class MigrateController extends \yii\console\controllers\MigrateController { public $templateFile = __DIR__ .'/../views/migration.php'; } 

It is clear that the route __DIR__ .'/../views/migration.php' is a new template. But yii2 doesn't see my override. It seems that somewhere you need to explicitly change the map of the monitored controllers. But where?

I use the yii2 advanced template.

  • Perhaps it is not necessary to do this by the controller. Look here for a new pattern for migration: yiiframework.ru/doc/guide/ru/database.migration .... or maybe you call the controller wrong .. not with that namespace, for example. - Alexey Shimansky
  • You gave a link to yii1, and I use yii2. The name is all right. Nearby is a controller that works fine. - Razzwan 4:02 pm
  • the essence is the same. global settings should be enough - Alexey Shimansky

1 answer 1

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' ], ],