From 1ef37d253d2bcc892bebc3eeaa2955d018998b32 Mon Sep 17 00:00:00 2001 From: oldrind <1401019000@qq.com> Date: Wed, 30 Mar 2016 10:11:09 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=AD=A3model=E7=B1=BBjoin?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E4=B8=8D=E8=83=BD=E8=AF=BB=E5=8F=96=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E8=A1=A8=E5=89=8D=E7=BC=80=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 16a0d302..4d7bb3e2 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1477,17 +1477,15 @@ class Model $prefix = $this->tablePrefix; // 传入的表名为数组 if (is_array($join)) { - if (0 !== key($join)) { - // 键名为表名,键值作为表的别名 - $table = key($join) . ' ' . current($join); + if (0 !== $key = key($join)) { + // 设置了键名则键名为表名,键值作为表的别名 + $table = $key . ' ' . array_shift($join); } else { - $table = current($join); + $table = array_shift($join); } - if (isset($join[1])) { - // 第二个元素为字符串则把第二元素作为表前缀 - if (is_string($join[1])) { - $table = $join[1] . $table; - } + if (count($join)) { + // 有设置第二个元素则把第二元素作为表前缀 + $table = (string) current($join) . $table; } else { // 加上默认的表前缀 $table = $prefix . $table; From e462a05a001353fa40f64034e9dacb640815dd74 Mon Sep 17 00:00:00 2001 From: oldrind <1401019000@qq.com> Date: Wed, 30 Mar 2016 10:37:05 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=A7=86=E5=9B=BE=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9where=E7=BB=84=E5=90=88=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E5=AD=97=E6=AE=B5=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/traits/model/View.php | 80 ++++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 15 deletions(-) diff --git a/library/traits/model/View.php b/library/traits/model/View.php index 872dc70c..d4a7c3ec 100644 --- a/library/traits/model/View.php +++ b/library/traits/model/View.php @@ -37,6 +37,8 @@ trait View if (isset($view['_table'])) { // 2011/10/17 添加实际表名定义支持 可以实现同一个表的视图 $tableName .= $view['_table']; + $prefix = $this->tablePrefix; + $tableName = preg_replace_callback("/__([A-Z_-]+)__/sU", function ($match) use ($prefix) {return $prefix . strtolower($match[1]);}, $tableName); } else { $tableName .= \think\Loader::table($name)->getTableName(); } @@ -73,14 +75,14 @@ trait View $val = $this->_checkFields($name, $val); foreach ($val as $key => $field) { if (is_numeric($key)) { - $viewFields[$k . '.' . $field] = $field; + $viewFields[$field] = $k . '.' . $field; } elseif ('_' != substr($key, 0, 1)) { // 以_开头的为特殊定义 if (false !== strpos($key, '*') || false !== strpos($key, '(') || false !== strpos($key, '.')) { //如果包含* 或者 使用了sql方法 则不再添加前面的表名 - $viewFields[$key] = $field; + $viewFields[$field] = $key; } else { - $viewFields[$k . '.' . $key] = $field; + $viewFields[$field] = $k . '.' . $key; } } } @@ -130,15 +132,63 @@ trait View if (is_array($where)) { $_where = []; foreach ($where as $field => $value) { - if (false !== $k = array_search($field, $viewFields, true)) { - // 存在视图字段 - $_where[$k] = $value; + if ('_complex' != $field) { + $value = [$field => $value]; + $parent = 1; } else { - $_where[$field] = $value; + $parent = 0; + } + foreach ($value as $key => $val) { + if (0 !== strpos($key, '_')) { + if ($items = preg_split('/[\|\&]/', $key, -1, PREG_SPLIT_OFFSET_CAPTURE)) { + // 倒序找出的字段数组,以便从后向前替换 + $items = array_reverse($items); + foreach ($items as $item) { + if (isset($viewFields[$item[0]])) { // 存在视图字段 + $key = substr_replace($key, $viewFields[$item[0]], $item[1], strlen($item[0])); + } + } + } + if (isset($val[0]) && 'exp' == $val[0] && !empty($val[1])) { + if (preg_match_all('/(?