先config.php配置用于跨库的数据库配置
//数据库配置
‘db_config1’ => [
‘type’ => ‘mysql’,
// 服务器地址
‘hostname’ => ‘127.0.0.1’,
// 数据库名
‘database’ => ‘’,
// 用户名
‘username’ => '’,
// 密码
‘password’ =>’*******’,
// 端口
‘hostport’ =>’’,
// 数据库编码默认采用utf8
‘charset’ => ‘utf8mb4’,
// 数据库表前缀
‘prefix’ => ‘cs_’,
]
然后模型配置跨库
<?php namespace app\admin\model\exam; use think\Model; use traits\model\SoftDelete; class Question extends Model { protected $connection = 'db_config1'; // 表名 protected $name = 'exam_question'; // 自动写入时间戳字段 protected $autoWriteTimestamp = 'int'; // 定义时间戳字段名 protected $createTime = 'createtime'; protected $updateTime = 'updatetime'; } 这样就可以直接实例化去查询了 关联就用 ::with(),但是数据量大的话不推荐这样查效率是硬伤,可以据索引单表取数据,用column,然后2表取出的数据拼接成数组