From f0f66d69ce5de36b344219464e17e546dbe8b11b Mon Sep 17 00:00:00 2001 From: anas-srikou Date: Thu, 27 Apr 2023 14:52:47 +0900 Subject: [PATCH 1/3] decode json field into php object --- src/Resources/Table.php | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Resources/Table.php b/src/Resources/Table.php index e0f541f..e1bf997 100644 --- a/src/Resources/Table.php +++ b/src/Resources/Table.php @@ -326,12 +326,41 @@ protected function runQuery($table, Builder $builder, $extras) } } + // Filter the columns list based on the field type we need + // loop through the result and convert the fields with the same specified type to object + $data = $this->decodeJsonField($schema, $result); + + return $data; + } + + /** + * @param TableSchema $schema + * @param Collection $result + * @return array + */ + private function decodeJsonField(TableSchema $schema, Collection $result): array { + $columns = $schema->getColumns(); + $nvcharColumns = []; + foreach ($columns as $column) { + if ($column->dbType !== "nvarchar") continue; + $nvcharColumns[] = $column->name; + } + if (!empty($nvcharColumns)) { + $temp = $result->map(function ($item) use ($nvcharColumns) { + foreach ($nvcharColumns as $column) { + // json_decode wil return object if the decode is success or null + // in case of null => meaning the value is not valid json then we return the original value + $item[$column] = json_decode($item[$column]) ?? $item[$column]; + } + return $item; + }); + $result = collect($temp); + } + $data = $result->toArray(); if (!empty($meta)) { $data['meta'] = $meta; } - - return $data; } /** From 5a085d49b67f3b6059356df61edf092c95e3bd5d Mon Sep 17 00:00:00 2001 From: Kevin McGahey <36458555+thekevinm@users.noreply.github.com> Date: Mon, 11 Dec 2023 13:54:40 -0800 Subject: [PATCH 2/3] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b0e32d4..ce7690b 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "dreamfactory/df-database": "~0.11" + "dreamfactory/df-database": "~1.1.0" }, "autoload": { "psr-4": { From 3cd7e14984a2543bf4dd09fd8df7b2d0167d1d86 Mon Sep 17 00:00:00 2001 From: nicdavidson Date: Mon, 11 Dec 2023 17:26:23 -0700 Subject: [PATCH 3/3] setting up class inheritance --- src/Resources/Table.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Resources/Table.php b/src/Resources/Table.php index e1bf997..9b24d3d 100644 --- a/src/Resources/Table.php +++ b/src/Resources/Table.php @@ -338,7 +338,7 @@ protected function runQuery($table, Builder $builder, $extras) * @param Collection $result * @return array */ - private function decodeJsonField(TableSchema $schema, Collection $result): array { + public function decodeJsonField(TableSchema $schema, Collection $result): array { $columns = $schema->getColumns(); $nvcharColumns = []; foreach ($columns as $column) { @@ -361,6 +361,7 @@ private function decodeJsonField(TableSchema $schema, Collection $result): array if (!empty($meta)) { $data['meta'] = $meta; } + return $data; } /**