CakePHP 动态切换数据库(使用相同的数据源)?

发布于 2024-10-29 05:15:38 字数 1432 浏览 2 评论 0原文

我试图构建一个小函数,用于在控制器中动态切换数据库,我必须仅使用一个数据源。

在我的database.php上:

function __construct() {

    $server = Configure::read('Server');

    if(!empty($server['database'])) $this->local['database'] = $server['database'];
    $this->default = $this->{$server['datasource']};

}

用于根据服务器配置切换数据库。它工作得很好。

我试图建立这个:

/**
 * Connects to specified database
 *
 * @param array $config Server config to use {datasource:?, database:?}
 * @return array db->config on success, false on failure
 * @access public
 */
function dbConnect($config = array()) {
    ClassRegistry::init('ConnectionManager');
    //debug($config['datasource']);
    //$dbInstance =& ConnectionManager::getInstance();
    //$dbInstance->config->{$config['datasource']}['database'] = $config['database'];

    $db =& ConnectionManager::getDataSource($config['datasource']);
    $db->disconnect();
    $db->cacheSources = false;
    $db->config['database'] = $config['database'];
    $db->config['persistent'] = false;
    debug($db->config);
    $db->connect();
    if(!$db->isConnected()) {
        $this->error('!$db->isConnected()');
        return false;
    }
    return $db->config;
}

但遗憾的是,一切似乎都有效,但我总是使用 $this->Player->find('list') 从同一个数据库获取数据。我尝试了 $this->Player->cacheQueries = false; 但没有成功。

I have tried to build a small function that would be use in controller to switch database on the fly, i must use only one datasource.

On my database.php :

function __construct() {

    $server = Configure::read('Server');

    if(!empty($server['database'])) $this->local['database'] = $server['database'];
    $this->default = $this->{$server['datasource']};

}

Is used to switch database depending on server config. It is working great.

I tried to build up this :

/**
 * Connects to specified database
 *
 * @param array $config Server config to use {datasource:?, database:?}
 * @return array db->config on success, false on failure
 * @access public
 */
function dbConnect($config = array()) {
    ClassRegistry::init('ConnectionManager');
    //debug($config['datasource']);
    //$dbInstance =& ConnectionManager::getInstance();
    //$dbInstance->config->{$config['datasource']}['database'] = $config['database'];

    $db =& ConnectionManager::getDataSource($config['datasource']);
    $db->disconnect();
    $db->cacheSources = false;
    $db->config['database'] = $config['database'];
    $db->config['persistent'] = false;
    debug($db->config);
    $db->connect();
    if(!$db->isConnected()) {
        $this->error('!$db->isConnected()');
        return false;
    }
    return $db->config;
}

But sadly, everything seems to work but i alwas get data from the same DB using $this->Player->find('list') for instance. I tried $this->Player->cacheQueries = false; with no more success.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反差帅 2024-11-05 05:15:38

使用它使其工作(动态创建新连接):

$newDbConfig = $this->dbConnect($serverConfig);
$this->Model->useDbConfig = $newDbConfig['name'];
$this->Model->cacheQueries = false;

使用:

/**
 * Connects to specified database
 *
 * @param array $config Server config to use {datasource:?, database:?}
 * @return array db->config on success, false on failure
 * @access public
 */
function dbConnect($config = array()) {
    ClassRegistry::init('ConnectionManager');

    $nds = $config['datasource'] . '_' . $config['database'];
    $db =& ConnectionManager::getDataSource($config['datasource']);
    $db->setConfig(array('name' => $nds, 'database' => $config['database'], 'persistent' => false));
    if($ds = ConnectionManager::create($nds, $db->config)) return $db->config;
    return false;

}

Made it work using this (create a new connection on the fly) :

$newDbConfig = $this->dbConnect($serverConfig);
$this->Model->useDbConfig = $newDbConfig['name'];
$this->Model->cacheQueries = false;

With :

/**
 * Connects to specified database
 *
 * @param array $config Server config to use {datasource:?, database:?}
 * @return array db->config on success, false on failure
 * @access public
 */
function dbConnect($config = array()) {
    ClassRegistry::init('ConnectionManager');

    $nds = $config['datasource'] . '_' . $config['database'];
    $db =& ConnectionManager::getDataSource($config['datasource']);
    $db->setConfig(array('name' => $nds, 'database' => $config['database'], 'persistent' => false));
    if($ds = ConnectionManager::create($nds, $db->config)) return $db->config;
    return false;

}
水中月 2024-11-05 05:15:38

我只是想告诉大家,现在更改当前模型的数据源(数据库连接)非常简单:

例如在我的 PersonsController 索引中:

public function index() {

    //change to a different datasource which is already written in App/Config/database.php-file
    $this->Person->setDataSource('testdb2');
    $persons = $this->Person->find('all');
    $this->set('db1persons', $persons);

    //change to a different database
    $this->Person->setDataSource('testdb');
    $persons = $this->Person->find('all');
    $this->set('db2persons', $persons);
}

如您所见,这里的关键是使用 $this->Model->setDataSource()

I just wanted to tell that nowadays it's really simple to change current model's datasource (database connection):

For example in my PersonsController index:

public function index() {

    //change to a different datasource which is already written in App/Config/database.php-file
    $this->Person->setDataSource('testdb2');
    $persons = $this->Person->find('all');
    $this->set('db1persons', $persons);

    //change to a different database
    $this->Person->setDataSource('testdb');
    $persons = $this->Person->find('all');
    $this->set('db2persons', $persons);
}

As you can see the key here is to use $this->Model->setDataSource()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文