duandiaoqian5795
duandiaoqian5795
2017-11-10 18:09

Yii2:如何指定多个数据库模式?

已采纳

I am using PostgreSQL and the default database schema in my Yii2 application.

I created a new schema called laboratory and I need to define it in the common/config/main-local.php file.

This is my current main-local.php file:

<?php
return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'pgsql:host=localhost;dbname=travel',
            'username' => 'aaaa',
            'password' => 'bbbb',
            'charset' => 'utf8',
        ],
    ],
];

How can I add the laboratory schema in this file? I need both schemas.

Is Yii2 supporting multiples schemas?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duanjian4698 duanjian4698 4年前

    It wasn't necessary to change it:

    <?php
    return [
        'components' => [
            'db' => [
                'class' => 'yii\db\Connection',
                'dsn' => 'pgsql:host=localhost;dbname=travel',
                'username' => 'aaaa',
                'password' => 'bbbb',
                'charset' => 'utf8',
            ],
        ],
    ];
    

    Then Gii code generator recognizes the laboratory schema (but autocomplete for Table Name doesn't work).

    点赞 评论 复制链接分享
  • douyijin7741 douyijin7741 4年前

    You can configure more than one in components

          return [
          'components' => [
              'db1' => [
                  'class' => 'yii\db\Connection',
                  'dsn' => 'mysql:host=localhost;dbname=testdb1',
                  'username' => 'demo1',
                  'password' => 'demo1',
              ],
              'db2' => [
                  'class' => 'yii\db\Connection',
                  'dsn' => 'mysql:host=localhost;dbname=testdb2',
                  'username' => 'demo2',
                  'password' => 'demo2',
              ],
    
          ],
      ];
    

    and you can refer to each one using

     \Yii::$app->db1;  
    
     or 
    
      \Yii::$app->db2;  
    

    http://www.yiiframework.com/doc-2.0/guide-db-active-record.html

    http://www.yiiframework.com/doc-2.0/guide-start-databases.html

    for postgresql you could try

          return [
          'components' => [
              'db1' => [
                  'class' => 'yii\db\Connection',
                  'dsn' => 'pgsql:host=localhost;dbname=testdb1',
                  'username' => 'demo1',
                  'password' => 'demo1',
                  'schemaMap' => [
                    'pgsql'=> [
                      'class'=>'yii\db\pgsql\Schema',
                      'defaultSchema' => 'your_schema1' //specify your schema here
                    ]
                  ],
              ],
              'db2' => [
                  'class' => 'yii\db\Connection',
                  'dsn' => 'mysql:host=localhost;dbname=testdb2',
                  'username' => 'demo2',
                  'password' => 'demo2',
                  'schemaMap' => [
                    'pgsql'=> [
                      'class'=>'yii\db\pgsql\Schema',
                      'defaultSchema' => 'your_schema2' //specify your schema here
                    ]
                  ],
              ],
    
          ],
      ];
    
    点赞 评论 复制链接分享

相关推荐