From 8b492e0ac06f9aaf92bdc7675886390bf6453c18 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 4 Jul 2016 15:44:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 44 ++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 8b7c5556..b68d7e54 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1717,8 +1717,8 @@ class Query * @param array|string|Query|\Closure $data * @return Collection|false|\PDOStatement|string * @throws DbException - * @throws Exception - * @throws PDOException + * @throws ModelNotFoundException + * @throws DataNotFoundException */ public function select($data = null) { @@ -1789,11 +1789,7 @@ class Query } } } elseif (!empty($options['fail'])) { - if (!empty($this->model)) { - throw new ModelNotFoundException('model data Not Found:' . $this->model, $this->model, $options); - } else { - throw new DataNotFoundException('table data not Found:' . $options['table'], $options['table'], $options); - } + $this->throwNotFound($options); } return $resultSet; } @@ -1804,8 +1800,8 @@ class Query * @param array|string|Query|\Closure $data * @return array|false|\PDOStatement|string|Model * @throws DbException - * @throws Exception - * @throws PDOException + * @throws ModelNotFoundException + * @throws DataNotFoundException */ public function find($data = null) { @@ -1874,25 +1870,37 @@ class Query } } } elseif (!empty($options['fail'])) { - if (!empty($this->model)) { - throw new ModelNotFoundException('model data Not Found:' . $this->model, $this->model, $options); - } else { - throw new DataNotFoundException('table data not Found:' . $options['table'], $options['table'], $options); - } + $this->throwNotFound($options); } else { $data = null; } return $data; } + /** + * 查询失败 抛出异常 + * @access public + * @param array $options 查询参数 + * @throws ModelNotFoundException + * @throws DataNotFoundException + */ + protected function throwNotFound($options = []) + { + if (!empty($this->model)) { + throw new ModelNotFoundException('model data Not Found:' . $this->model, $this->model, $options); + } else { + throw new DataNotFoundException('table data not Found:' . $options['table'], $options['table'], $options); + } + } + /** * 查找多条记录 如果不存在则抛出异常 * @access public * @param array|string|Query|\Closure $data * @return array|\PDOStatement|string|Model * @throws DbException - * @throws Exception - * @throws PDOException + * @throws ModelNotFoundException + * @throws DataNotFoundException */ public function selectOrFail($data = null) { @@ -1905,8 +1913,8 @@ class Query * @param array|string|Query|\Closure $data * @return array|\PDOStatement|string|Model * @throws DbException - * @throws Exception - * @throws PDOException + * @throws ModelNotFoundException + * @throws DataNotFoundException */ public function findOrFail($data = null) {