改进主从读取

This commit is contained in:
thinkphp
2018-04-25 15:01:48 +08:00
parent 0ad7586556
commit 4003537d84
3 changed files with 19 additions and 9 deletions

View File

@@ -183,7 +183,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @param bool $master 是否从主库读取 * @param bool $master 是否从主库读取
* @return void * @return void
*/ */
public function readMaster($master) public function readMaster($master = true)
{ {
if ($master) { if ($master) {
static::$readMaster[$this->class] = true; static::$readMaster[$this->class] = true;

View File

@@ -451,24 +451,24 @@ abstract class Connection
$this->debug(false); $this->debug(false);
if ($query && !empty($this->config['deploy']) && !empty($this->config['read_master'])) { if ($query && !empty($this->config['deploy']) && !empty($this->config['read_master'])) {
$query->setModelReadMaster(true); $query->readMaster(true);
} }
$this->numRows = $this->PDOStatement->rowCount(); $this->numRows = $this->PDOStatement->rowCount();
return $this->numRows; return $this->numRows;
} catch (\PDOException $e) { } catch (\PDOException $e) {
if ($this->isBreak($e)) { if ($this->isBreak($e)) {
return $this->close()->execute($sql, $bind); return $this->close()->execute($sql, $bind, $query);
} }
throw new PDOException($e, $this->config, $this->getLastsql()); throw new PDOException($e, $this->config, $this->getLastsql());
} catch (\Throwable $e) { } catch (\Throwable $e) {
if ($this->isBreak($e)) { if ($this->isBreak($e)) {
return $this->close()->execute($sql, $bind); return $this->close()->execute($sql, $bind, $query);
} }
throw $e; throw $e;
} catch (\Exception $e) { } catch (\Exception $e) {
if ($this->isBreak($e)) { if ($this->isBreak($e)) {
return $this->close()->execute($sql, $bind); return $this->close()->execute($sql, $bind, $query);
} }
throw $e; throw $e;
} }

View File

@@ -53,6 +53,8 @@ class Query
protected static $info = []; protected static $info = [];
// 回调事件 // 回调事件
private static $event = []; private static $event = [];
// 读取主库
private static $readMaster = [];
/** /**
* 构造函数 * 构造函数
@@ -141,16 +143,20 @@ class Query
} }
/** /**
* 设置模型从主库读取数据 * 设置后续从主库读取数据
* @access public * @access public
* @param bool $master * @param bool $master
* @return void * @return void
*/ */
public function setModelReadMaster($master = true) public function readMaster($master = true)
{ {
if ($this->model) { if ($master) {
$this->model->readMaster($master); $table = isset($this->options['table']) ? $this->options['table'] : $this->getTable();
static::$readMaster[$table] = true;
} }
return $this;
} }
/** /**
@@ -2936,6 +2942,10 @@ class Query
} }
} }
if (isset(static::$readMaster[$options['table']])) {
$options['master'] = true;
}
foreach (['join', 'union', 'group', 'having', 'limit', 'order', 'force', 'comment'] as $name) { foreach (['join', 'union', 'group', 'having', 'limit', 'order', 'force', 'comment'] as $name) {
if (!isset($options[$name])) { if (!isset($options[$name])) {
$options[$name] = ''; $options[$name] = '';