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": { diff --git a/src/Resources/Table.php b/src/Resources/Table.php index e0f541f..9b24d3d 100644 --- a/src/Resources/Table.php +++ b/src/Resources/Table.php @@ -326,11 +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 + */ + public 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; }