duandiaoqian5795
2017-11-10 18:09
浏览 232
已采纳

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?

图片转代码服务由CSDN问答提供 功能建议

我在Yii2应用程序中使用PostgreSQL和默认数据库模式。

我创建了一个名为 laboratory 的新模式,我需要在common / config / main-local.php文件中定义它。

这是我当前的< strong> main-local.php 文件:

 &lt;?php 
return [
'components'=&gt;  [
'db'=&gt;  [
'class'=&gt;  'yii \ db \ Connection',
'dsn'=&gt;  'pgsql:host = localhost; dbname = travel',
'username'=&gt;  'aaaa',
'密码'=&gt;  'bbbb',
'charset'=&gt;  'utf8',
],
],
]; 
   
 
 

如何在此文件中添加实验室模式? 我需要两个模式。

Yii2是否支持多个模式?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanjian4698 2017-11-13 12:00
    已采纳

    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 2017-11-10 18:12

    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
                    ]
                  ],
              ],
    
          ],
      ];
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题