Commit 94b85828 authored by Taylor Otwell's avatar Taylor Otwell

Add DateTime support to database binding layer.

Signed-off-by: 's avatarTaylor Otwell <taylorotwell@gmail.com>
parent 7d5b6b37
...@@ -209,6 +209,19 @@ class Connection { ...@@ -209,6 +209,19 @@ class Connection {
$sql = $this->grammar()->shortcut($sql, $bindings); $sql = $this->grammar()->shortcut($sql, $bindings);
// Next we need to translate all DateTime bindings to their date-time
// strings that are compatible with the database. Each grammar may
// define it's own date-time format according to its needs.
$datetime = $this->grammar()->datetime;
for ($i = 0; $i < count($bindings); $i++)
{
if ($bindings[$i] instanceof \DateTime)
{
$bindings[$i] = $bindings[$i]->format($datetime);
}
}
// Each database operation is wrapped in a try / catch so we can wrap // Each database operation is wrapped in a try / catch so we can wrap
// any database exceptions in our custom exception class, which will // any database exceptions in our custom exception class, which will
// set the message to include the SQL and query bindings. // set the message to include the SQL and query bindings.
......
...@@ -193,7 +193,7 @@ abstract class Model { ...@@ -193,7 +193,7 @@ abstract class Model {
{ {
$model = new static(array(), true); $model = new static(array(), true);
if (static::$timestamps) $attributes['updated_at'] = $model->get_timestamp(); if (static::$timestamps) $attributes['updated_at'] = new \DateTime;
return $model->query()->where($model->key(), '=', $id)->update($attributes); return $model->query()->where($model->key(), '=', $id)->update($attributes);
} }
...@@ -405,21 +405,11 @@ abstract class Model { ...@@ -405,21 +405,11 @@ abstract class Model {
*/ */
protected function timestamp() protected function timestamp()
{ {
$this->updated_at = static::get_timestamp(); $this->updated_at = new \DateTime;
if ( ! $this->exists) $this->created_at = $this->updated_at; if ( ! $this->exists) $this->created_at = $this->updated_at;
} }
/**
* Get the current timestamp in its storable form.
*
* @return mixed
*/
public static function get_timestamp()
{
return date('Y-m-d H:i:s');
}
/** /**
* Get a new fluent query builder instance for the model. * Get a new fluent query builder instance for the model.
* *
......
...@@ -204,7 +204,7 @@ class Has_Many_And_Belongs_To extends Relationship { ...@@ -204,7 +204,7 @@ class Has_Many_And_Belongs_To extends Relationship {
{ {
if (Pivot::$timestamps) if (Pivot::$timestamps)
{ {
$attributes['created_at'] = $this->model->get_timestamp(); $attributes['created_at'] = new \DateTime;
$attributes['updated_at'] = $attributes['created_at']; $attributes['updated_at'] = $attributes['created_at'];
} }
......
...@@ -29,7 +29,7 @@ class Has_One_Or_Many extends Relationship { ...@@ -29,7 +29,7 @@ class Has_One_Or_Many extends Relationship {
{ {
if ($this->model->timestamps()) if ($this->model->timestamps())
{ {
$attributes['updated_at'] = $this->model->get_timestamp(); $attributes['updated_at'] = new \DateTime;
} }
return $this->table->update($attributes); return $this->table->update($attributes);
......
...@@ -5,6 +5,13 @@ use Laravel\Database\Expression; ...@@ -5,6 +5,13 @@ use Laravel\Database\Expression;
class Grammar extends \Laravel\Database\Grammar { class Grammar extends \Laravel\Database\Grammar {
/**
* The format for properly saving a DateTime.
*
* @var string
*/
public $datetime = 'Y-m-d H:i:s';
/** /**
* All of the query componenets in the order they should be built. * All of the query componenets in the order they should be built.
* *
......
...@@ -11,6 +11,13 @@ class SQLServer extends Grammar { ...@@ -11,6 +11,13 @@ class SQLServer extends Grammar {
*/ */
protected $wrapper = '[%s]'; protected $wrapper = '[%s]';
/**
* The format for properly saving a DateTime.
*
* @var string
*/
public $datetime = 'Y-m-d H:i:s.000';
/** /**
* Compile a SQL SELECT statement from a Query instance. * Compile a SQL SELECT statement from a Query instance.
* *
......
...@@ -368,7 +368,7 @@ class Postgres extends Grammar { ...@@ -368,7 +368,7 @@ class Postgres extends Grammar {
*/ */
protected function type_date(Fluent $column) protected function type_date(Fluent $column)
{ {
return 'TIMESTAMP'; return 'TIMESTAMP(0) WITHOUT TIME ZONE';
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment