改进Query类

This commit is contained in:
thinkphp
2016-07-04 15:44:08 +08:00
parent d6daa4aca8
commit 8b492e0ac0

View File

@@ -1717,8 +1717,8 @@ class Query
* @param array|string|Query|\Closure $data * @param array|string|Query|\Closure $data
* @return Collection|false|\PDOStatement|string * @return Collection|false|\PDOStatement|string
* @throws DbException * @throws DbException
* @throws Exception * @throws ModelNotFoundException
* @throws PDOException * @throws DataNotFoundException
*/ */
public function select($data = null) public function select($data = null)
{ {
@@ -1789,11 +1789,7 @@ class Query
} }
} }
} elseif (!empty($options['fail'])) { } elseif (!empty($options['fail'])) {
if (!empty($this->model)) { $this->throwNotFound($options);
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);
}
} }
return $resultSet; return $resultSet;
} }
@@ -1804,8 +1800,8 @@ class Query
* @param array|string|Query|\Closure $data * @param array|string|Query|\Closure $data
* @return array|false|\PDOStatement|string|Model * @return array|false|\PDOStatement|string|Model
* @throws DbException * @throws DbException
* @throws Exception * @throws ModelNotFoundException
* @throws PDOException * @throws DataNotFoundException
*/ */
public function find($data = null) public function find($data = null)
{ {
@@ -1874,25 +1870,37 @@ class Query
} }
} }
} elseif (!empty($options['fail'])) { } elseif (!empty($options['fail'])) {
if (!empty($this->model)) { $this->throwNotFound($options);
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);
}
} else { } else {
$data = null; $data = null;
} }
return $data; 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 * @access public
* @param array|string|Query|\Closure $data * @param array|string|Query|\Closure $data
* @return array|\PDOStatement|string|Model * @return array|\PDOStatement|string|Model
* @throws DbException * @throws DbException
* @throws Exception * @throws ModelNotFoundException
* @throws PDOException * @throws DataNotFoundException
*/ */
public function selectOrFail($data = null) public function selectOrFail($data = null)
{ {
@@ -1905,8 +1913,8 @@ class Query
* @param array|string|Query|\Closure $data * @param array|string|Query|\Closure $data
* @return array|\PDOStatement|string|Model * @return array|\PDOStatement|string|Model
* @throws DbException * @throws DbException
* @throws Exception * @throws ModelNotFoundException
* @throws PDOException * @throws DataNotFoundException
*/ */
public function findOrFail($data = null) public function findOrFail($data = null)
{ {