Skip to content

Commit

Permalink
fix: json field array writing #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Chance-fyi committed Oct 21, 2022
1 parent 8e3d3d5 commit 201085c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
"phpunit/phpunit": "10.0.x-dev",
"illuminate/database": "^8.0",
"topthink/think-orm": "2.0.x-dev"
},
"require": {
"ext-json": "*"
}
}
8 changes: 7 additions & 1 deletion src/orm/illuminate/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ public function getChangedAttributes($model): array
public function getValue($model, string $key): string
{
$keyText = $key . "_text";
return (string)($model->$keyText ?? $model->$key);
$value = $model->$keyText ?? $model->$key;

if (is_array($value)) {
return json_encode($value, JSON_UNESCAPED_UNICODE);
} else {
return (string)$value;
}
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/orm/think/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ public function getValue($model, string $key): string
$value = $model->$keyText ?? $model->$key;

if (is_array($value)) {
return implode(" ", $value);
return json_encode($value, JSON_UNESCAPED_UNICODE);
} elseif ($value instanceof Raw) {
return $value->getValue();
} else {
return $value;
return (string)$value;
}
}

Expand All @@ -106,7 +106,13 @@ public function getOldValue($model, string $key): string
{
$keyText = $key . "_text";
$attributeFun = "get" . Str::studly(Str::lower($keyText)) . "Attr";
return (string)(method_exists($model, $attributeFun) ? $model->$attributeFun($model->getOrigin($key)) : $model->getOrigin($key));
$value = method_exists($model, $attributeFun) ? $model->$attributeFun($model->getOrigin($key)) : $model->getOrigin($key);

if (is_array($value)) {
return json_encode($value, JSON_UNESCAPED_UNICODE);
} else {
return (string)$value;
}
}

/**
Expand Down

0 comments on commit 201085c

Please sign in to comment.