mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Adding create() method to create DataSources dynamically; Ticket #1068
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3195 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
baf2364c83
commit
21be313929
1 changed files with 38 additions and 14 deletions
|
@ -169,27 +169,51 @@ class ConnectionManager extends Object {
|
|||
$connections = get_object_vars($_this->config);
|
||||
|
||||
if ($connections != null) {
|
||||
|
||||
foreach($connections as $name => $config) {
|
||||
|
||||
if (!isset($config['datasource'])) {
|
||||
$config['datasource'] = 'dbo';
|
||||
}
|
||||
|
||||
if (isset($config['driver']) && $config['driver'] != null && !empty($config['driver'])) {
|
||||
$filename = $config['datasource'] . DS . $config['datasource'] . '_' . $config['driver'];
|
||||
$classname = Inflector::camelize(strtolower($config['datasource'] . '_' . $config['driver']));
|
||||
} else {
|
||||
$filename = $config['datasource'] . '_source';
|
||||
$classname = Inflector::camelize(strtolower($config['datasource'] . '_source'));
|
||||
}
|
||||
$_this->_connectionsEnum[$name] = array('filename' => $filename, 'classname' => $classname);
|
||||
$_this->_connectionsEnum[$name] = $_this->__getDriver($config);
|
||||
}
|
||||
return $this->_connectionsEnum;
|
||||
} else {
|
||||
$this->cakeError('missingConnection', array(array('className' => 'ConnectionManager')));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Dynamically creates a DataSource object at runtime, with the given name and settings
|
||||
*
|
||||
* @param string $name The DataSource name
|
||||
* @param array $config The DataSource configuration settings
|
||||
* @return object A reference to the DataSource object, or null if creation failed
|
||||
*/
|
||||
function &create($name = '', $config = array()) {
|
||||
$_this =& ConnectionManager::getInstance();
|
||||
|
||||
if (empty($name) || empty($config) || array_key_exists($name, $_this->_connectionsEnum)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$_this->config->{$name} = $config;
|
||||
$_this->_connectionsEnum[$name] = $_this->__getDriver($config);
|
||||
return $_this->getDataSource($name);
|
||||
}
|
||||
/**
|
||||
* Private method
|
||||
*
|
||||
* Returns the file and class name for the given driver
|
||||
*/
|
||||
function __getDriver($config) {
|
||||
if (!isset($config['datasource'])) {
|
||||
$config['datasource'] = 'dbo';
|
||||
}
|
||||
|
||||
if (isset($config['driver']) && $config['driver'] != null && !empty($config['driver'])) {
|
||||
$filename = $config['datasource'] . DS . $config['datasource'] . '_' . $config['driver'];
|
||||
$classname = Inflector::camelize(strtolower($config['datasource'] . '_' . $config['driver']));
|
||||
} else {
|
||||
$filename = $config['datasource'] . '_source';
|
||||
$classname = Inflector::camelize(strtolower($config['datasource'] . '_source'));
|
||||
}
|
||||
return array('filename' => $filename, 'classname' => $classname);
|
||||
}
|
||||
/**
|
||||
* Destructor.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue