设置了主从服务器后,支持从主服务器读数据

方法是在模型类中调用 readMaster方法
$this->readMaster()->select();

该方法当此调用有效
This commit is contained in:
thinkphp
2015-06-12 15:21:08 +08:00
parent 956be17494
commit cb9faf2aa9
2 changed files with 14 additions and 3 deletions

View File

@@ -130,10 +130,11 @@ abstract class Driver {
* @access public * @access public
* @param string $str sql指令 * @param string $str sql指令
* @param boolean $fetchSql 不执行只是获取SQL * @param boolean $fetchSql 不执行只是获取SQL
* @param boolean $master 是否在主服务器读操作
* @return mixed * @return mixed
*/ */
public function query($str,$fetchSql=false) { public function query($str,$fetchSql=false,$master=false) {
$this->initConnect(false); $this->initConnect($master);
if ( !$this->_linkID ) return false; if ( !$this->_linkID ) return false;
$this->queryStr = $str; $this->queryStr = $str;
if(!empty($this->bind)){ if(!empty($this->bind)){
@@ -935,7 +936,7 @@ abstract class Driver {
$this->model = $options['model']; $this->model = $options['model'];
$this->parseBind(!empty($options['bind'])?$options['bind']:array()); $this->parseBind(!empty($options['bind'])?$options['bind']:array());
$sql = $this->buildSelectSql($options); $sql = $this->buildSelectSql($options);
$result = $this->query($sql,!empty($options['fetch_sql']) ? true : false); $result = $this->query($sql,!empty($options['fetch_sql']) ? true : false,!empty($options['read_master']) ? true : false);
return $result; return $result;
} }

View File

@@ -1317,4 +1317,14 @@ class Model {
} }
return $this; return $this;
} }
/**
* 设置从主服务器读取数据
* @access public
* @return Model
*/
public function readMaster(){
$this->options['read_master'] = true;
return $this;
}
} }