Skip to content

Instantly share code, notes, and snippets.

@Ryadnov
Created July 17, 2012 09:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ryadnov/3128285 to your computer and use it in GitHub Desktop.
Save Ryadnov/3128285 to your computer and use it in GitHub Desktop.
Переключение между серверами DB
<?php
require_once(dirname(__FILE__) . '/protected/components/WebApplication.php');
Yii::createApplication('WebApplication', dirname(__FILE__) . '/protected/config/main.php')->run();
<?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',
),
),
// ...
);
<?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