Merge remote-tracking branch 'origin/master'

This commit is contained in:
yunwuxin
2018-02-26 15:31:27 +08:00
5 changed files with 60 additions and 31 deletions

View File

@@ -64,14 +64,16 @@ class Template
*/ */
public function __construct(array $config = []) public function __construct(array $config = [])
{ {
$this->config['cache_path'] = TEMP_PATH; $this->config['cache_path'] = TEMP_PATH;
$this->config = array_merge($this->config, $config); $this->config = array_merge($this->config, $config);
$this->config['taglib_begin_origin'] = $this->config['taglib_begin']; $this->config['taglib_begin_origin'] = $this->config['taglib_begin'];
$this->config['taglib_end_origin'] = $this->config['taglib_end']; $this->config['taglib_end_origin'] = $this->config['taglib_end'];
$this->config['taglib_begin'] = $this->stripPreg($this->config['taglib_begin']);
$this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']); $this->config['taglib_begin'] = preg_quote($this->config['taglib_begin'], '/');
$this->config['tpl_begin'] = $this->stripPreg($this->config['tpl_begin']); $this->config['taglib_end'] = preg_quote($this->config['taglib_end'], '/');
$this->config['tpl_end'] = $this->stripPreg($this->config['tpl_end']); $this->config['tpl_begin'] = preg_quote($this->config['tpl_begin'], '/');
$this->config['tpl_end'] = preg_quote($this->config['tpl_end'], '/');
// 初始化模板编译存储器 // 初始化模板编译存储器
$type = $this->config['compile_type'] ? $this->config['compile_type'] : 'File'; $type = $this->config['compile_type'] ? $this->config['compile_type'] : 'File';
@@ -79,20 +81,6 @@ class Template
$this->storage = new $class(); $this->storage = new $class();
} }
/**
* 字符串替换 避免正则混淆
* @access private
* @param string $str
* @return string
*/
private function stripPreg($str)
{
return str_replace(
['{', '}', '(', ')', '|', '[', ']', '-', '+', '*', '.', '^', '?'],
['\{', '\}', '\(', '\)', '\|', '\[', '\]', '\-', '\+', '\*', '\.', '\^', '\?'],
$str);
}
/** /**
* 模板变量赋值 * 模板变量赋值
* @access public * @access public

View File

@@ -403,7 +403,7 @@ class Query
if (empty($this->options['table'])) { if (empty($this->options['table'])) {
$this->options['table'] = $this->getTable(); $this->options['table'] = $this->getTable();
} }
$key = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($this->options) . serialize($this->bind)); $key = is_string($cache['key']) ? $cache['key'] : md5($this->connection->getConfig('database') . '.' . $field . serialize($this->options) . serialize($this->bind));
$result = Cache::get($key); $result = Cache::get($key);
} }
if (false === $result) { if (false === $result) {
@@ -447,7 +447,7 @@ class Query
if (empty($this->options['table'])) { if (empty($this->options['table'])) {
$this->options['table'] = $this->getTable(); $this->options['table'] = $this->getTable();
} }
$guid = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($this->options) . serialize($this->bind)); $guid = is_string($cache['key']) ? $cache['key'] : md5($this->connection->getConfig('database') . '.' . $field . serialize($this->options) . serialize($this->bind));
$result = Cache::get($guid); $result = Cache::get($guid);
} }
if (false === $result) { if (false === $result) {
@@ -793,7 +793,7 @@ class Query
} }
if (isset($this->options['field'])) { if (isset($this->options['field'])) {
$field = array_merge($this->options['field'], $field); $field = array_merge((array) $this->options['field'], $field);
} }
$this->options['field'] = array_unique($field); $this->options['field'] = array_unique($field);
return $this; return $this;
@@ -2322,7 +2322,7 @@ class Query
// 判断查询缓存 // 判断查询缓存
$cache = $options['cache']; $cache = $options['cache'];
unset($options['cache']); unset($options['cache']);
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options) . serialize($this->bind)); $key = is_string($cache['key']) ? $cache['key'] : md5($this->connection->getConfig('database') . '.' . serialize($options) . serialize($this->bind));
$resultSet = Cache::get($key); $resultSet = Cache::get($key);
} }
if (false === $resultSet) { if (false === $resultSet) {
@@ -2423,10 +2423,12 @@ class Query
} elseif (is_array($value) && is_string($value[0]) && 'eq' == strtolower($value[0])) { } elseif (is_array($value) && is_string($value[0]) && 'eq' == strtolower($value[0])) {
$data = $value[1]; $data = $value[1];
} }
$prefix = $this->connection->getConfig('database') . '.';
if (isset($data)) { if (isset($data)) {
return 'think:' . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data; return 'think:' . $prefix . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data;
} else { } else {
return md5(serialize($options) . serialize($bind)); return md5($prefix . serialize($options) . serialize($bind));
} }
} }
@@ -2463,11 +2465,11 @@ class Query
// 判断查询缓存 // 判断查询缓存
$cache = $options['cache']; $cache = $options['cache'];
if (true === $cache['key'] && !is_null($data) && !is_array($data)) { if (true === $cache['key'] && !is_null($data) && !is_array($data)) {
$key = 'think:' . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data; $key = 'think:' . $this->connection->getConfig('database') . '.' . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data;
} elseif (is_string($cache['key'])) { } elseif (is_string($cache['key'])) {
$key = $cache['key']; $key = $cache['key'];
} elseif (!isset($key)) { } elseif (!isset($key)) {
$key = md5(serialize($options) . serialize($this->bind)); $key = md5($this->connection->getConfig('database') . '.' . serialize($options) . serialize($this->bind));
} }
$result = Cache::get($key); $result = Cache::get($key);
} }

View File

@@ -128,6 +128,7 @@ class BelongsTo extends OneToOne
} }
if (!empty($range)) { if (!empty($range)) {
$this->query->removeWhereField($localKey);
$data = $this->eagerlyWhere($this->query, [ $data = $this->eagerlyWhere($this->query, [
$localKey => [ $localKey => [
'in', 'in',
@@ -171,7 +172,8 @@ class BelongsTo extends OneToOne
{ {
$localKey = $this->localKey; $localKey = $this->localKey;
$foreignKey = $this->foreignKey; $foreignKey = $this->foreignKey;
$data = $this->eagerlyWhere($this->query, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure); $this->query->removeWhereField($localKey);
$data = $this->eagerlyWhere($this->query, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure);
// 关联模型 // 关联模型
if (!isset($data[$result->$foreignKey])) { if (!isset($data[$result->$foreignKey])) {
$relationModel = null; $relationModel = null;
@@ -220,4 +222,21 @@ class BelongsTo extends OneToOne
return $this->parent->setRelation($this->relation, null); return $this->parent->setRelation($this->relation, null);
} }
/**
* 执行基础查询(仅执行一次)
* @access protected
* @return void
*/
protected function baseQuery()
{
if (empty($this->baseQuery)) {
if (isset($this->parent->{$this->foreignKey})) {
// 关联查询带入关联条件
$this->query->where($this->localKey, '=', $this->parent->{$this->foreignKey});
}
$this->baseQuery = true;
}
}
} }

View File

@@ -130,7 +130,8 @@ class HasOne extends OneToOne
} }
if (!empty($range)) { if (!empty($range)) {
$data = $this->eagerlyWhere($this, [ $this->query->removeWhereField($foreignKey);
$data = $this->eagerlyWhere($this->query, [
$foreignKey => [ $foreignKey => [
'in', 'in',
$range, $range,
@@ -172,7 +173,8 @@ class HasOne extends OneToOne
{ {
$localKey = $this->localKey; $localKey = $this->localKey;
$foreignKey = $this->foreignKey; $foreignKey = $this->foreignKey;
$data = $this->eagerlyWhere($this, [$foreignKey => $result->$localKey], $foreignKey, $relation, $subRelation, $closure); $this->query->removeWhereField($foreignKey);
$data = $this->eagerlyWhere($this->query, [$foreignKey => $result->$localKey], $foreignKey, $relation, $subRelation, $closure);
// 关联模型 // 关联模型
if (!isset($data[$result->$localKey])) { if (!isset($data[$result->$localKey])) {
@@ -190,4 +192,20 @@ class HasOne extends OneToOne
} }
} }
/**
* 执行基础查询(仅执行一次)
* @access protected
* @return void
*/
protected function baseQuery()
{
if (empty($this->baseQuery)) {
if (isset($this->parent->{$this->localKey})) {
// 关联查询带入关联条件
$this->query->where($this->foreignKey, '=', $this->parent->{$this->localKey});
}
$this->baseQuery = true;
}
}
} }

View File

@@ -304,6 +304,8 @@ abstract class OneToOne extends Relation
*/ */
protected function eagerlyWhere($model, $where, $key, $relation, $subRelation = '', $closure = false) protected function eagerlyWhere($model, $where, $key, $relation, $subRelation = '', $closure = false)
{ {
$this->baseQuery = true;
// 预载入关联查询 支持嵌套预载入 // 预载入关联查询 支持嵌套预载入
if ($closure) { if ($closure) {
call_user_func_array($closure, [ & $model]); call_user_func_array($closure, [ & $model]);