Skip to content

Commit

Permalink
fix:修复json字段写入字符串null的bug
Browse files Browse the repository at this point in the history
mysql中json字段默认值为null的情况下,设置json字段会出现写入字符串null的情况
  • Loading branch information
liuqiandev authored and liu21st committed Jan 11, 2024
1 parent 7b0b8ea commit 99cee09
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/db/Builder.php
Expand Up @@ -63,6 +63,10 @@ protected function parseData(Query $query, array $data = [], array $fields = [],
if ($val instanceof Raw) {
$result[$item] = $this->parseRaw($query, $val);
continue;
}elseif (is_null($val)) {
// json字段默认为NULL,需要优先处理,不然会出现json字段写入字符串null的情况
$result[$item] = 'NULL';
continue;
} elseif (!is_scalar($val) && (in_array($key, (array) $query->getOptions('json')) || 'json' == $query->getFieldType($key))) {
$val = json_encode($val);
}
Expand All @@ -76,8 +80,6 @@ protected function parseData(Query $query, array $data = [], array $fields = [],
if ($options['strict']) {
throw new Exception('fields not exists:[' . $key . ']');
}
} elseif (is_null($val)) {
$result[$item] = 'NULL';
} elseif (is_array($val) && !empty($val) && is_string($val[0])) {
if (in_array(strtoupper($val[0]), ['INC', 'DEC'])) {
$result[$item] = match (strtoupper($val[0])) {
Expand Down

0 comments on commit 99cee09

Please sign in to comment.