mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 07:12:47 +08:00
Query类增加failException方法 用于设置查询没有找到数据时候是否抛出异常
This commit is contained in:
@@ -89,7 +89,7 @@ abstract class Builder
|
|||||||
$result = [];
|
$result = [];
|
||||||
foreach ($data as $key => $val) {
|
foreach ($data as $key => $val) {
|
||||||
if (!in_array($key, $fields, true)) {
|
if (!in_array($key, $fields, true)) {
|
||||||
if ($this->connection->getConfig('fields_strict')) {
|
if ($options['strict']) {
|
||||||
throw new Exception(' fields not exists :[' . $key . ']');
|
throw new Exception(' fields not exists :[' . $key . ']');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -572,7 +572,7 @@ abstract class Builder
|
|||||||
foreach ($dataSet as &$data) {
|
foreach ($dataSet as &$data) {
|
||||||
foreach ($data as $key => $val) {
|
foreach ($data as $key => $val) {
|
||||||
if (!in_array($key, $fields, true)) {
|
if (!in_array($key, $fields, true)) {
|
||||||
if ($this->connection->getConfig('fields_strict')) {
|
if ($options['strict']) {
|
||||||
throw new Exception(' fields not exists :[' . $key . ']');
|
throw new Exception(' fields not exists :[' . $key . ']');
|
||||||
}
|
}
|
||||||
unset($data[$key]);
|
unset($data[$key]);
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ use PDO;
|
|||||||
use think\Cache;
|
use think\Cache;
|
||||||
use think\Db;
|
use think\Db;
|
||||||
use think\Exception;
|
use think\Exception;
|
||||||
|
use think\exception\DbException;
|
||||||
use think\Loader;
|
use think\Loader;
|
||||||
use think\Model;
|
use think\Model;
|
||||||
use think\model\Relation;
|
use think\model\Relation;
|
||||||
@@ -795,6 +796,30 @@ class Query
|
|||||||
return $this;
|
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
|
* @access public
|
||||||
@@ -1138,6 +1163,8 @@ class Query
|
|||||||
$resultSet = $result->eagerlyResultSet($resultSet, $options['with']);
|
$resultSet = $result->eagerlyResultSet($resultSet, $options['with']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} elseif (!empty($options['fail'])) {
|
||||||
|
throw new DbException('Data not Found', $options, $sql);
|
||||||
}
|
}
|
||||||
return $resultSet;
|
return $resultSet;
|
||||||
}
|
}
|
||||||
@@ -1209,6 +1236,8 @@ class Query
|
|||||||
$data->eagerlyResult($data, $options['with']);
|
$data->eagerlyResult($data, $options['with']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} elseif (!empty($options['fail'])) {
|
||||||
|
throw new DbException('Data not Found', $options, $sql);
|
||||||
} else {
|
} else {
|
||||||
$data = false;
|
$data = false;
|
||||||
}
|
}
|
||||||
@@ -1320,6 +1349,10 @@ class Query
|
|||||||
$options['field'] = '*';
|
$options['field'] = '*';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isset($options['strict'])) {
|
||||||
|
$options['strict'] = $this->connection->getConfig('fields_strict');
|
||||||
|
}
|
||||||
|
|
||||||
foreach (['master', 'lock', 'fetch_pdo', 'fetch_sql', 'distinct'] as $name) {
|
foreach (['master', 'lock', 'fetch_pdo', 'fetch_sql', 'distinct'] as $name) {
|
||||||
if (!isset($options[$name])) {
|
if (!isset($options[$name])) {
|
||||||
$options[$name] = false;
|
$options[$name] = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user