diff --git a/dibi/libs/DibiConnection.php b/dibi/libs/DibiConnection.php index 5c10660c..94d0ab38 100644 --- a/dibi/libs/DibiConnection.php +++ b/dibi/libs/DibiConnection.php @@ -480,7 +480,8 @@ class DibiConnection extends DibiObject */ public function createResultSet(IDibiResultDriver $resultDriver) { - return new DibiResult($resultDriver, $this->config['result']); + $res = new DibiResult($resultDriver); + return $res->setFormat(dibi::DATETIME, $this->config['result']['formatDateTime']); } diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index 14a2062c..4005f628 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -54,23 +54,18 @@ class DibiResult extends DibiObject implements IDataSource /** @var string returned object class */ private $rowClass = 'DibiRow'; - /** @var string date-time format */ - private $dateFormat = ''; + /** @var array format */ + private $formats = array(); /** * @param IDibiResultDriver - * @param array */ - public function __construct($driver, $config) + public function __construct($driver) { $this->driver = $driver; $this->detectTypes(); - - if (!empty($config['formatDateTime'])) { - $this->dateFormat = is_string($config['formatDateTime']) ? $config['formatDateTime'] : ''; - } } @@ -525,18 +520,18 @@ class DibiResult extends DibiObject implements IDataSource } elseif ($type === dibi::DATE || $type === dibi::DATETIME) { if ((int) $value === 0) { // '', NULL, FALSE, '0000-00-00', ... - } elseif ($this->dateFormat === '') { // return DateTime object (default) + } elseif (empty($this->formats[$type])) { // return DateTime object (default) $row[$key] = new DibiDateTime(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value); - } elseif ($this->dateFormat === 'U') { // return timestamp + } elseif ($this->formats[$type] === 'U') { // return timestamp $row[$key] = is_numeric($value) ? (int) $value : strtotime($value); } elseif (is_numeric($value)) { // formatted date - $row[$key] = date($this->dateFormat, $value); + $row[$key] = date($this->formats[$type], $value); } else { $value = new DibiDateTime($value); - $row[$key] = $value->format($this->dateFormat); + $row[$key] = $value->format($this->formats[$type]); } } elseif ($type === dibi::BINARY) { @@ -572,6 +567,31 @@ class DibiResult extends DibiObject implements IDataSource + /** + * Sets data format. + * @param string type (use constant Dibi::*) + * @param string format + * @return DibiResult provides a fluent interface + */ + final public function setFormat($type, $format) + { + $this->formats[$type] = $format; + return $this; + } + + + + /** + * Returns data format. + * @return string + */ + final public function getFormat($type) + { + return isset($this->formats[$type]) ? $this->formats[$type] : NULL; + } + + + /********************* meta info ****************d*g**/ diff --git a/examples/result-set-data-types.php b/examples/result-set-data-types.php index f67d3093..5e38bbc9 100644 --- a/examples/result-set-data-types.php +++ b/examples/result-set-data-types.php @@ -20,7 +20,9 @@ dibi::connect(array( $res = dibi::query('SELECT * FROM [customers]'); $res->setType('customer_id', Dibi::INTEGER) - ->setType('added', Dibi::DATETIME); + ->setType('added', Dibi::DATETIME) + ->setFormat(dibi::DATETIME, 'Y-m-d H:i:s'); + Debugger::dump( $res->fetch() ); // outputs: