改进主从读取

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

@@ -53,6 +53,8 @@ class Query
protected static $info = [];
// 回调事件
private static $event = [];
// 读取主库
private static $readMaster = [];
/**
* 构造函数
@@ -141,16 +143,20 @@ class Query
}
/**
* 设置模型从主库读取数据
* 设置后续从主库读取数据
* @access public
* @param bool $master
* @return void
*/
public function setModelReadMaster($master = true)
public function readMaster($master = true)
{
if ($this->model) {
$this->model->readMaster($master);
if ($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) {
if (!isset($options[$name])) {
$options[$name] = '';