yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

Module Migration failing #19611

Closed TheFox closed 2 years ago

TheFox commented 2 years ago

What steps will reproduce the problem?

I created a simple Module under modules\dsr_setup namespace. I also created a Migration under that modules namespace for migrations, using yii migrate/create modules\\dsr_setup\\migrations\\Mailer.

I mapped the namespace in the Controller Map.

return [
    ...
    'controllerMap' => [
        'migrate' => [
            ...
            'migrationNamespaces' => [
                'modules\dsr_setup\migrations',
            ],
            ...
        ],
    ],
    ...
];

When I want to enable the module using yii module/enable dsr-setup the Migrations fail.

What is the expected result?

I expect that the migrations are working while enabling the module.

What do you get instead?

Error Message


Total 1 new migration to be applied:
    M221006094347Mailer

*** applying M221006094347Mailer
Exception 'yii\di\NotInstantiableException' with message 'Failed to instantiate component or class "M221006094347Mailer".'

in /app/protected/vendor/yiisoft/yii2/di/Container.php:509

Caused by: Exception 'ReflectionException' with message 'Class "M221006094347Mailer" does not exist'

in /app/protected/vendor/yiisoft/yii2/di/Container.php:507

Stack trace: ...

Additional info

Q A
Yii version 2.0.46-dev
PHP version 8.1.10
Operating system Debian 11
bizley commented 2 years ago

What is the namespace of the M221006094347Mailer class?

TheFox commented 2 years ago

@bizley namespace modules\dsr_setup\migrations;

bizley commented 2 years ago

Does it work when you add to the migrate config 'migrationPath' => null?

TheFox commented 2 years ago

That does not make any difference.

bizley commented 2 years ago

I'm not sure what does module/enable do? Does the migration work if you run it using yii migrate?

WinterSilence commented 2 years ago
'controllerMap' => [
    // Application migrations:
    'migrate' => [
        'class' => yii\console\controllers\MigrateController::class,
        'migrationPath' => null,
        'migrationNamespaces' => ['modules\\dsr_setup\\migrations'],
    ],
    // Yii migrations:
    'yii-migrate' => [
        'class' => yii\console\controllers\MigrateController::class,
        'migrationPath' => [
            '@yii/web/migrations',
            // Note: update component's configuration before uncomment next migrations
            // '@yii/rbac/migrations',
            // '@yii/caching/migrations',
            // '@yii/i18n/migrations',
            // '@yii/log/migrations',
        ]
    ],
]

yii migrate/create modules\\dsr_setup\\migrations\\Mailer is invalid command, call something like yii migrate/create create_mailer_table or yii migrate/create modules\\dsr_setup\\migrations\\createMailerTable

TheFox commented 2 years ago

Seems like module/enable is not an original Yii 2 command. I guess this comes with the application (HumHub) I am using Yii 2 with. When I use yii migrate it's working.

@WinterSilence

yii migrate/create modules\dsr_setup\migrations\Mailer is invalid command

Why is it an invalid command? It worked for me to create the class file.

bizley commented 2 years ago

If so I'm afraid you must take it to the HumHub for help. I'm closing this, please let me know if I can help with anything regarding Yii itself.

WinterSilence commented 2 years ago

@TheFox command migrate use special syntax, see guide for more info. You must add alias for namespace modules: Yii::setAlias('@modules', '@app/modules') in bootstrap class of module because file path generate as https://github.com/yiisoft/yii2/blob/0b4741ea6aa2fa58866c4eb65ef17c0b8d03323a/framework/console/controllers/BaseMigrateController.php#L741

TheFox commented 2 years ago

@bizley Ok I see. Thank you for your help.

@WinterSilence There is an alias for that. When I use a namespace in a module the migration Class Name begins with an capital letter M.

For example, when I am using yii migrate/create modules\\dsr_setup\\migrations\\makeSomeChanges it creates a class named M221006144332MakeSomeChanges. The format is a little bit different to yii migrate/create makeSomeChanges. It creates a m221006_145135_makeSomeChanges class.

WinterSilence commented 2 years ago

@TheFox as I'm already told, you can use syntax like as yii migrate/create modules\\dsr_setup\\migrations\\createMailerTable. Look PHPDoc to actionCreate https://github.com/yiisoft/yii2/blob/0b4741ea6aa2fa58866c4eb65ef17c0b8d03323a/framework/console/controllers/BaseMigrateController.php#L619