From 0ed23d6186560d72a020f4eced2ec552d25944b1 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 23 Apr 2016 18:16:26 +0800 Subject: [PATCH] =?UTF-8?q?Query=E7=B1=BB=E5=A2=9E=E5=8A=A0failException?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20=E7=94=A8=E4=BA=8E=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=B2=A1=E6=9C=89=E6=89=BE=E5=88=B0=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=97=B6=E5=80=99=E6=98=AF=E5=90=A6=E6=8A=9B=E5=87=BA?= =?UTF-8?q?=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Builder.php | 4 ++-- library/think/db/Query.php | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/library/think/db/Builder.php b/library/think/db/Builder.php index 3d5be97a..67b2813d 100644 --- a/library/think/db/Builder.php +++ b/library/think/db/Builder.php @@ -89,7 +89,7 @@ abstract class Builder $result = []; foreach ($data as $key => $val) { if (!in_array($key, $fields, true)) { - if ($this->connection->getConfig('fields_strict')) { + if ($options['strict']) { throw new Exception(' fields not exists :[' . $key . ']'); } } else { @@ -572,7 +572,7 @@ abstract class Builder foreach ($dataSet as &$data) { foreach ($data as $key => $val) { if (!in_array($key, $fields, true)) { - if ($this->connection->getConfig('fields_strict')) { + if ($options['strict']) { throw new Exception(' fields not exists :[' . $key . ']'); } unset($data[$key]); diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 6929d69b..d50f0ab6 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -15,6 +15,7 @@ use PDO; use think\Cache; use think\Db; use think\Exception; +use think\exception\DbException; use think\Loader; use think\Model; use think\model\Relation; @@ -795,6 +796,30 @@ class Query return $this; } + /** + * 设置是否严格检查字段名 + * @access public + * @param bool $strict 是否严格检查字段 + * @return $this + */ + public function strict($strict = true) + { + $this->options['strict'] = $strict; + return $this; + } + + /** + * 设置查询数据不存在是否抛出异常 + * @access public + * @param bool $fail 是否严格检查字段 + * @return $this + */ + public function failException($fail = true) + { + $this->options['fail'] = $fail; + return $this; + } + /** * 指定当前模型 * @access public @@ -1138,6 +1163,8 @@ class Query $resultSet = $result->eagerlyResultSet($resultSet, $options['with']); } } + } elseif (!empty($options['fail'])) { + throw new DbException('Data not Found', $options, $sql); } return $resultSet; } @@ -1209,6 +1236,8 @@ class Query $data->eagerlyResult($data, $options['with']); } } + } elseif (!empty($options['fail'])) { + throw new DbException('Data not Found', $options, $sql); } else { $data = false; } @@ -1320,6 +1349,10 @@ class Query $options['field'] = '*'; } + if (!isset($options['strict'])) { + $options['strict'] = $this->connection->getConfig('fields_strict'); + } + foreach (['master', 'lock', 'fetch_pdo', 'fetch_sql', 'distinct'] as $name) { if (!isset($options[$name])) { $options[$name] = false;