Yii2: Can't reconfigure application component for a module

Created on 13 Jan 2018  路  11Comments  路  Source: yiisoft/yii2

I try to change single property of DB component for my module as described here: http://www.yiiframework.com/doc-2.0/guide-structure-modules.html#accessing-components-from-within-modules

I'm expecting to have the same DB connection in my module as in the application but just with different table prefix

Instead I get an error "The configuration for the "db" component must contain a "class" element."

| Q | A
| ---------------- | ---
| Yii version | 2.0.13.1
| PHP version | 7.0
| Operating system | Windows 10

docs

Most helpful comment

It is about tree traversal, and apparently it is a bit unclear ..
The example shows 2 partial configuration, the merging is an assumption due to forementioned unclarity.

The idea is that you can replace configuration, merging is not intended nor desired here.

I'll create a PR for the documentation soon.

All 11 comments

Thanks for posting in our issue tracker.
In order to properly assist you, we need additional information:

  • When does the issue occur?
  • What do you see?
  • What was the expected result?
  • Can you supply us with a stacktrace? (optional)
  • Do you have exact code to reproduce it? Maybe a PHPUnit tests that fails? (optional)

Thanks!

_This is an automated comment, triggered by adding the label status:need more info._

Not sure if module specific component configs get merged with the application or not but the error says you've not specified a class name.

Does your main config have the class key -> value?

'modules' => [
    'mymodule' => [
        'components' => [
            'db' => [
                'class' => 'yii\db\Connection',
                'tablePrefix' => 'module_',
            ],
        ],
    ],
],

Yes, class name is specified in common/config/main.php

'components' => [
    'db' => [
        'class'     => \yii\db\Connection::class,
        'dsn'       => 'mysql:host=localhost;dbname=mydb',
    ],
]

And then in modules/mymodule/config.php

'components' => [
    'db' => [
        'tablePrefix' => 'mymodule_',
    ],
]

Module config is loaded in modules/mymodule/Module.php

public function init()
{
    parent::init();

    // Initialize the module with the configuration loaded from config.php
    $path = $this->basePath . '/config.php';
    if (file_exists($path)) {
        \yii::configure($this, require $path);
    }
}

I don't think configs are merged.

Configs are not deeply-merged.

According to documentation they should be merged: http://www.yiiframework.com/doc-2.0/guide-structure-modules.html#accessing-components-from-within-modules

I have misunderstood the original issue description.
This feature was introduced in PR https://github.com/yiisoft/yii2/pull/14350 by @SamMousa

Unfortunately, the documented behavior is not tested. @uaoleg could you submit a PR with failing test?

@SilverFire my PR was not about config merging, there was a different issue for this, where I came to the conclusion that merging is not desired: https://github.com/yiisoft/yii2/issues/15089

Then what is this documentation about? https://github.com/yiisoft/yii2/pull/14350/files#diff-89d108ebaf09973620d6127f7210be31R299

It is about tree traversal, and apparently it is a bit unclear ..
The example shows 2 partial configuration, the merging is an assumption due to forementioned unclarity.

The idea is that you can replace configuration, merging is not intended nor desired here.

I'll create a PR for the documentation soon.

Was this page helpful?
0 / 5 - 0 ratings