我试图从循环中连接多个数据库,但看到 CakePHP 无法更改 database
,只能更改其他信息(如用户/密码/主机).
app/Config/database.php
app/Controller/CronController.php
$companys = $this->Company->find('all');foreach($companys as $company) {$设置=数组('数据源' =>'数据库/Mysql','主机' =>$company['Company']['host'],'登录' =>$company['公司']['用户名'],'密码' =>$company['公司']['密码'],'数据库' =>$company['Company']['database'],);ConnectionManager::drop('client');$db = ConnectionManager::create('client', $settings);尝试 {调试($this->MyModel->find('first'));} 捕获(异常 $e){echo '';echo "异常: ", $e->getMessage(), "
";/*调试($this->MyModel->getDataSource());输出:[...][配置] =>大批([持久] =>[主持人] =>0.0.0.0//正确的主机[登录] =>root//正确登录[密码] =>pass//正确的密码[数据库] =>数据库1[端口] =>3306[数据源] =>数据库/Mysql[前缀] =>[编码] =>utf8)[...]*