web.php配置,引入或者直接设置db
我的是引入
$db = require __DIR__ . ‘/db.php’;
components下设置
‘db’ => $db[‘db’],
‘db2’ => $db[‘db2’],
db.php文件,设置成自己的数据库配置
<?php return [ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => env('db_dsn'), 'username' => env('db_username'), 'password' => env('db_password'), 'charset' => env('db_charset'), ], 'db2' => [ 'class' => 'yii\db\Connection', 'dsn' => env('db2_dsn'), 'username' => env('db2_username'), 'password' => env('db2_password'), 'charset' => env('db2_charset'), ], // Schema cache options (for production environment) 'enableSchemaCache' => env('db_enableSchemaCache'), 'schemaCacheDuration' => env('db_schemaCacheDuration'), 'schemaCache' => env('db_schemaCache'), 'enableProfiling'=>true, ];
View Code
在模型中使用特定的db连接
public static function getDb() { return Yii::$app->db2; }
View Code
测试了一下,如果不设置,默认连接的是db
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。