3

could someone explain me how can I correctly specify my modules migration namespaces? As I see in the documentation, it's:

return [
'controllerMap' => [
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationNamespaces' => [
            'app\migrations', // Common migrations for the whole application
            'module\migrations', // Migrations for the specific project's module
            'some\extension\migrations', // Migrations for the specific extension
            ],
        ],
    ],
];

But there are no explanation in which file should I write the commands. I've tried it in config.php, as:

    'controllerMap' => [
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationNamespaces' => [
            'app\modules\adBoard\migrations',
        ],

But I don't know which controller class should I write. Could someone tell me in which file I have to specify it and how to specify it correctly?

HELPME
  • 714
  • 14
  • 35

3 Answers3

1

If you refer to this documentation

Configuring Command Globally

Instead of entering the same option values every time you run the migration command, you may configure it once for all in the application configuration like shown below:

return [
     'controllerMap' => [
         'migrate' => [
             'class' => 'yii\console\controllers\MigrateController',
             'migrationTable' => 'backend_migration',
         ],
     ], ];

With the above configuration, each time you run the migration command, the backend_migration table will be used to record the migration history. You no longer need to specify it via the migrationTable command-line option.

Namespaced Migrations

Since 2.0.10 you can use namespaces for the migration classes. You can specify the list of the migration namespaces via migrationNamespaces. Using of the namespaces for migration classes allows you usage of the several source locations for the migrations. For example:

 return [
     'controllerMap' => [
         'migrate' => [
             'class' => 'yii\console\controllers\MigrateController',
             'migrationNamespaces' => [
                 'app\migrations', // Common migrations for the whole application
                 'module\migrations', // Migrations for the specific project's module
                 'some\extension\migrations', // Migrations for the specific extension
             ],
         ],
     ], ];

this config should be placed in you console/config/main.php

but for namespaced migration remeber that starting for 2.0.10

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
1

depending on your yii2-template application (basic or advanced) the location of "console" specific settings are located in different directories.

For basic template, console fetch settings from <app>/config/console.php file.

And for advanced template, you should edit <app>/console/config/main.php file.

Remember that your settings for console will not affect web-settings, so if you want to register some component in the whole project, you have to duplicate it in both files.

P.S. I would like to add one more detail about advanced template, is that it has common setting for frontend and backend sub-apps, which is located in <app>/common/config/main.php, but those settings are not common with console commands.

Yerke
  • 2,187
  • 3
  • 19
  • 33
  • Just tried to enter my mentioned commands to `console.php` file (because of the `basic` template), but didn't worked :( I cannot run my migration because of the namespace, when I delete it, it's working – HELPME Jun 27 '17 at 07:11
1

I would like to share my experience with Yii2 namespaced migrations.

Scenario

  1. I am using advanced template.
  2. I have 100s of old migrations in console/migrations folder.
  3. I have new extension which have namespaced migrations.
  4. I have new migrations in old folder console/migrations.
  5. I want to create future migrations with namespaces in new folder console/migrations/namespaced. I want to keep all old migrations intact which are located at console/migrations.

console/config/main.php configurations worked for me.

return [
'controllerMap' => [
    'migrate' => [
        'class' => \yii\console\controllers\MigrateController::class,
        'migrationNamespaces' => [
            'console\migrations\namespaced',
            'yii\swiftsmser\migrations'
        ]
    ],
],
//.... more configurations
];

With above configurations, when I executed yii migrate, It includes all above mentioned folders.

Note: To create new migration. Just make sure to use command like below.

yii migrate/create console\\migrations\\namespaced\\DLTTemplatesForSMS

Terry
  • 307
  • 1
  • 12