问题
I have working on a project which contains two DB , the common tables are in main DB and some specific tables are in separate DB.
I need to connect the DB dynamically because the second DB is based on the user they log-in in site . We get the details from DB and then create a DB settings and connect .
Can anyone help on this concept.
回答1:
i have used this method
function dbConnect($config = array()) {
$config['datasource'] = 'test';
ClassRegistry::init('ConnectionManager');
// echo "<pre>";print_R($config);die;
// echo $nds = $config['datasource'] . '_' . $config['dbname'];
$nds = $config['datasource'] . '_' . $config['dbname'];
$db =& ConnectionManager::getDataSource($config['datasource']);
$dbprfx = $config['dbname'].'_';
$db->setConfig(array('name' => $nds, 'database' => $config['dbname'],'prefix' => $dbprfx, 'persistent' => false));
if($ds = ConnectionManager::create($nds, $db->config)) return $db->config;
return false;
}
which works fine
thanks for all replies.
来源:https://stackoverflow.com/questions/20917647/how-to-connect-two-db-in-cakephp