如何在 yii2 中正确指定迁移命名空间 类?

How to correctly specify migration namespace classes in yii2?

有人可以解释一下我怎样才能正确指定我的 modules migration namespaces 吗?正如我在文档中看到的那样,它是:

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

但是没有说明我应该把命令写在哪个文件中。我在 config.php 中尝试过,如:

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

但是我不知道应该写哪个控制器class。有人可以告诉我必须在哪个文件中指定它以及如何正确指定它吗?

如果你参考这个文档

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

这个配置应该放在你console/config/main.php

但对于命名空间迁移,请记住从 2.0.10

开始

根据您的 yii2-template 应用程序(basicadvanced),"console" 特定设置的位置位于不同的目录中。

对于基本模板,控制台从<app>/config/console.php文件获取设置。

对于高级模板,您应该编辑<app>/console/config/main.php文件。

请记住,您对控制台的设置不会影响网络设置,因此如果您想在整个项目中注册某些组件,则必须在两个文件中复制它。

P.S。我想添加一个关于 advanced 模板的更多细节,它具有 frontendbackend[=30] 的通用设置=] 子应用程序,位于 <app>/common/config/main.php,但这些设置在控制台命令中并不常见。

我想分享我在 Yii2 命名空间迁移方面的经验。

场景

  1. 我正在使用高级模板。
  2. 我在 console/migrations 文件夹中有 100 多个旧迁移。
  3. 我有新的扩展,它有命名空间迁移。
  4. 我在旧文件夹 console/migrations 中有新的迁移。
  5. 我想在新文件夹 console/migrations/namespaced 中创建带有命名空间的未来迁移。我想保持位于 console/migrations.
  6. 的所有旧迁移完好无损

console/config/main.php 配置对我有用。

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

通过以上配置,当我执行yii migrate时,它包含了上述所有文件夹。

注意:创建新迁移。请确保使用如下命令。

yii migrate/create console\migrations\namespaced\DLTTemplatesForSMS