how to connect two DB in cakephp

本小妞迷上赌 提交于 2019-12-25 02:27:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!