From cb9faf2aa928dbb6117b963fc98f46cd5ff0bf34 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 12 Jun 2015 15:21:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E4=BA=86=E4=B8=BB=E4=BB=8E?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E5=90=8E=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=BB=8E=E4=B8=BB=E6=9C=8D=E5=8A=A1=E5=99=A8=E8=AF=BB=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=20=E6=96=B9=E6=B3=95=E6=98=AF=E5=9C=A8=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E7=B1=BB=E4=B8=AD=E8=B0=83=E7=94=A8=20readMaster?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20$this->readMaster()->select();?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 该方法当此调用有效 --- library/think/db/driver.php | 7 ++++--- library/think/model.php | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/library/think/db/driver.php b/library/think/db/driver.php index 0005674c..6abb9204 100644 --- a/library/think/db/driver.php +++ b/library/think/db/driver.php @@ -130,10 +130,11 @@ abstract class Driver { * @access public * @param string $str sql指令 * @param boolean $fetchSql 不执行只是获取SQL + * @param boolean $master 是否在主服务器读操作 * @return mixed */ - public function query($str,$fetchSql=false) { - $this->initConnect(false); + public function query($str,$fetchSql=false,$master=false) { + $this->initConnect($master); if ( !$this->_linkID ) return false; $this->queryStr = $str; if(!empty($this->bind)){ @@ -935,7 +936,7 @@ abstract class Driver { $this->model = $options['model']; $this->parseBind(!empty($options['bind'])?$options['bind']:array()); $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; } diff --git a/library/think/model.php b/library/think/model.php index f1635a0b..70c5272a 100644 --- a/library/think/model.php +++ b/library/think/model.php @@ -1317,4 +1317,14 @@ class Model { } return $this; } + + /** + * 设置从主服务器读取数据 + * @access public + * @return Model + */ + public function readMaster(){ + $this->options['read_master'] = true; + return $this; + } } \ No newline at end of file