Created
February 25, 2020 02:52
-
-
Save tr4nk/3b0e793641de8933c501f08a3745aa07 to your computer and use it in GitHub Desktop.
Cake2 AppModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('Model', 'Model'); | |
class AppModel extends Model | |
{ | |
/** | |
* Connects to specified database | |
* | |
* @param String name of different database to connect with. | |
* @param String name of existing datasource | |
* @return boolean true on success, false on failure | |
*/ | |
public function setDatabase($database, $datasource = 'default') | |
{ | |
$newDatasource = $datasource . '_' . $database; | |
try { | |
$db = ConnectionManager::getDataSource($newDatasource); | |
$this->useDbConfig = $newDatasource; | |
$this->cacheQueries = false; | |
return true; | |
} catch (Exception $e) { | |
// debug($e->getMessage()); | |
} | |
$db = ConnectionManager::getDataSource($datasource); | |
$db->setConfig(array( | |
'name' => $newDatasource, | |
'database' => $database, | |
'persistent' => false | |
)); | |
if ( $ds = ConnectionManager::create($newDatasource, $db->config) ) { | |
$this->useDbConfig = $newDatasource; | |
$this->cacheQueries = false; | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment