Created
July 17, 2012 09:20
-
-
Save Ryadnov/3128285 to your computer and use it in GitHub Desktop.
Переключение между серверами DB
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 | |
require_once(dirname(__FILE__) . '/protected/components/WebApplication.php'); | |
Yii::createApplication('WebApplication', dirname(__FILE__) . '/protected/config/main.php')->run(); |
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 | |
return array( | |
// ... | |
'components' => array( | |
'db' => array( | |
'enabled' => false | |
), | |
'db1' => array( | |
'class' => 'CDbConnection', | |
'connectionString' => 'mysql:host=localhost;dbname=XXX', | |
'username' => 'XXX', | |
'password' => 'XXX', | |
), | |
'db2' => array( | |
'class' => 'CDbConnection', | |
'connectionString' => 'mysql:host=localhost;dbname=XXX', | |
'username' => 'XXX', | |
'password' => 'XXX', | |
), | |
), | |
// ... | |
); |
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 | |
class WebApplication extends CWebApplication { | |
private $_db_component; | |
public function getDb() { | |
if ($this->_db_component === null) { | |
try { | |
$this->_db_component = 'db1'; | |
$db = $this->getComponent('db1'); | |
$db->setActive(true); | |
return $db; | |
} catch (Exception $e) { | |
$this->_db_component = 'db2'; | |
return $this->getComponent('db2'); | |
} | |
} | |
return $this->getComponent($this->_db_component); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment