2

I read this question yii 2.0 multiple database connection, and use the answer of @Ali MasudianPour.

I follow the first step:

First you need to configure your databases like below:

return [
    'components' => [
        'db1' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=database1',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
        'db2' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=database2',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
    ],
]; ?>

But in my configuration in my db it gives me this error:

The configuration for the "db" component must contain a "class" element.

Community
  • 1
  • 1
Paul
  • 59
  • 1
  • 7

2 Answers2

4

This is because db component is main and required and you simply omitted its declaration.

Rename db1 and db2 for example to db and db1 accordingly:

return [
    'components' => [
        // Main connection
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=database1',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
        // Another connection
        'db1' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=database2',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
    ],
]; ?>

Update:

Note that for the basic application db is configured in separate file config/db.php and then required in main config config/web.php like so:

'db' => require(__DIR__ . '/db.php'),

So you can configure main connection in db.php and add additional below as db1.

arogachev
  • 33,150
  • 7
  • 114
  • 117
  • 1
    it gives the same result. `The configuration for the "db" component must contain a "class" element.` – Paul May 27 '15 at 06:22
  • It can't be the same, as you can see in changed config, `class` is included in `db ` component configuration. I recommend you to check if it's overrided in other application or local config files. – arogachev May 27 '15 at 06:29
  • how can I know if it's override in other application or local config files? can you give me example ? – Paul May 27 '15 at 06:34
  • What application you are using? Basic or advanced? – arogachev May 27 '15 at 06:35
  • My application I use: Basic – Paul May 27 '15 at 06:38
  • Seems like it's not the reason in this case. Check that you renamed it properly again. – arogachev May 27 '15 at 06:40
  • i just copied your answer and change the database name, i think there's no problem with that. i tried to use my old config. but it's just for 1 database , the syntax is like this: ` return [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=database1, 'username' => 'root', 'password' => '', 'charset' => 'utf8', ];` it works when i used like that , i don't know why it's not reading the `class` inside component and db. – Paul May 27 '15 at 06:46
  • Updated answer with some explanation for basic application. – arogachev May 27 '15 at 06:50
  • it has error `Setting read-only property: yii\web\Application::db` – Paul May 27 '15 at 06:58
  • 1
    Who downvoted? This should be the right answer. +1. Btw I think that for `db` the `class` property can be ommitted? – iamawebgeek May 27 '15 at 07:03
  • It can. "db" is by default assumed to be a `\yii\db\Connection` – Blizz May 27 '15 at 07:08
  • @Blizz i just add `'db' => require(__DIR__ . '/db.php'),` in main config `config/web.php` like you said, and tried to run the code, but still got this error : **The configuration for the "db" component must contain a "class" element.** – Paul May 27 '15 at 09:10
  • I also using Basic template, and tried following this answer, but I still get the same error message. – Blackjack Nov 30 '17 at 15:31
3

Simply you just have to create separate file for each database .

  1. config/db ->

    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=database1',
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8',
    

    To access another db create file in config named db1.php add add another db configuration.

  2. config/db1 ->

    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=database2',
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8',
    
  3. In config->web.php add

    'db' => require(__DIR__ . '/db.php'),
    'db1' => require(__DIR__ . '/db1.php'),
    
  4. To access the Model:

    public static function getDb() {
         return Yii::$app->db1;
    }
    
agold
  • 6,140
  • 9
  • 38
  • 54