35 public $slaves = array();
51 public function init()
55 $this->slaves[] = $this;
72 if ($this->enableSlave && !$this->getCurrentTransaction() && self::isReadOperation($sql)) {
73 return $this->
getSlave()->createCommand($sql);
87 if (!isset($this->_slave)) {
88 $slaves = $this->slaves;
92 foreach ($slaves as $slaveConfig) {
95 $slave = $slaveConfig;
96 } elseif (!isset($slaveConfig[
'class'])) {
99 foreach ($slaveConfig as $prop => $val) {
100 $slave->$prop = $val;
103 $slave = Yii::createComponent($slaveConfig);
105 Yii::app()->setComponent(
'dbslave', $slave);
106 $this->_slave = $slave;
107 Yii::log(
'Slave connection: ' . $this->_slave->connectionString, CLogger::LEVEL_TRACE,
'system.db.CDbCommand');
109 }
catch (Exception $e) {
110 Yii::log(
'Create slave database connection failed!', CLogger::LEVEL_WARNING);
115 if (!$this->_slave) {
116 $this->_slave = clone $this;
118 $this->_slave->enableSlave =
false;
120 return $this->_slave;
135 return preg_match(
'/^\s*(SELECT|SHOW|DESCRIBE|PRAGMA)/i', $sql);