CakePHP 动态切换数据库(使用相同的数据源)?
我试图构建一个小函数,用于在控制器中动态切换数据库,我必须仅使用一个数据源。
在我的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用它使其工作(动态创建新连接):
使用:
Made it work using this (create a new connection on the fly) :
With :
我只是想告诉大家,现在更改当前模型的数据源(数据库连接)非常简单:
例如在我的 PersonsController 索引中:
如您所见,这里的关键是使用 $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:
As you can see the key here is to use $this->Model->setDataSource()