Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
32 changes: 31 additions & 1 deletion src/Resources/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down