mirror of
https://github.com/dg/dibi.git
synced 2025-08-05 13:47:33 +02:00
Connection: added option [result][formatJson] that sets json column decoding (text|object|array) (#335)
This commit is contained in:
@@ -43,6 +43,11 @@ class Connection implements IConnection
|
||||
* - lazy (bool) => if true, connection will be established only when required
|
||||
* - result (array) => result set options
|
||||
* - formatDateTime => date-time format (if empty, DateTime objects will be returned)
|
||||
* - formatJson => json format (
|
||||
* "string" for leaving value as is,
|
||||
* "object" for decoding json as \stdClass,
|
||||
* "array" for decoding json as an array - default
|
||||
* )
|
||||
* - profiler (array)
|
||||
* - run (bool) => enable profiler?
|
||||
* - file => file to log
|
||||
@@ -59,6 +64,7 @@ class Connection implements IConnection
|
||||
Helpers::alias($config, 'result|formatDateTime', 'resultDateTime');
|
||||
$config['driver'] = $config['driver'] ?? 'mysqli';
|
||||
$config['name'] = $name;
|
||||
$config['result']['formatJson'] = $config['result']['formatJson'] ?? 'array';
|
||||
$this->config = $config;
|
||||
|
||||
// profiler
|
||||
@@ -395,7 +401,8 @@ class Connection implements IConnection
|
||||
{
|
||||
$res = new Result($resultDriver);
|
||||
return $res->setFormat(Type::DATE, $this->config['result']['formatDate'])
|
||||
->setFormat(Type::DATETIME, $this->config['result']['formatDateTime']);
|
||||
->setFormat(Type::DATETIME, $this->config['result']['formatDateTime'])
|
||||
->setFormat(Type::JSON, $this->config['result']['formatJson']);
|
||||
}
|
||||
|
||||
|
||||
|
@@ -496,7 +496,11 @@ class Result implements IDataSource
|
||||
$row[$key] = is_string($value) ? $this->getResultDriver()->unescapeBinary($value) : $value;
|
||||
|
||||
} elseif ($type === Type::JSON) {
|
||||
$row[$key] = json_decode($value, true);
|
||||
if ($this->formats[$type] === 'string') {
|
||||
$row[$key] = $value;
|
||||
} else {
|
||||
$row[$key] = json_decode($value, $this->formats[$type] === 'array');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user