1,配置文件web.php
'components' => [
'db' => require(__DIR__ . '/db.php'),
'db1' => require(__DIR__ . '/db1.php'),
]
2,db,db1配置 -- 可以根据需要将db.php db1.php 合并
db配置
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db', --数据库db
'username' => 'root',
'password' => 'test',
'charset' => 'utf8',
'tablePrefix'=>'db_'
];
db1配置
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db1', -- 数据库db1
'username' => 'root',
'password' => 'test',
'charset' => 'utf8',
'tablePrefix'=>'db1_'
];
3,查询
1,
Yii::$app->db->createCommand('select * from db.tableName')->queryAll();
Yii::$app->db1->createCommand('select * from db1.tableName')->queryAll();
2,
$db1 = Yii::$app->db1;
$query=(new \yii\db\Query())->select(['id','name'])->where('id=1')->from('add_log');
$data = $query->all($db1);------这里写的是Yii::$app->db1;
print_r($data);die;