diff --git a/laravel/database/connection.php b/laravel/database/connection.php index 5b2ec0e9..f146ebb9 100644 --- a/laravel/database/connection.php +++ b/laravel/database/connection.php @@ -209,6 +209,19 @@ class Connection { $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 // any database exceptions in our custom exception class, which will // set the message to include the SQL and query bindings. diff --git a/laravel/database/eloquent/model.php b/laravel/database/eloquent/model.php index 0bfeec77..2539d50d 100644 --- a/laravel/database/eloquent/model.php +++ b/laravel/database/eloquent/model.php @@ -193,7 +193,7 @@ abstract class Model { { $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); } @@ -405,21 +405,11 @@ abstract class Model { */ protected function timestamp() { - $this->updated_at = static::get_timestamp(); + $this->updated_at = new \DateTime; 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. * diff --git a/laravel/database/eloquent/relationships/has_many_and_belongs_to.php b/laravel/database/eloquent/relationships/has_many_and_belongs_to.php index b7b4dbb7..7a2bee1f 100644 --- a/laravel/database/eloquent/relationships/has_many_and_belongs_to.php +++ b/laravel/database/eloquent/relationships/has_many_and_belongs_to.php @@ -204,7 +204,7 @@ class Has_Many_And_Belongs_To extends Relationship { { if (Pivot::$timestamps) { - $attributes['created_at'] = $this->model->get_timestamp(); + $attributes['created_at'] = new \DateTime; $attributes['updated_at'] = $attributes['created_at']; } diff --git a/laravel/database/eloquent/relationships/has_one_or_many.php b/laravel/database/eloquent/relationships/has_one_or_many.php index 9df2fb05..274a5f3d 100644 --- a/laravel/database/eloquent/relationships/has_one_or_many.php +++ b/laravel/database/eloquent/relationships/has_one_or_many.php @@ -29,7 +29,7 @@ class Has_One_Or_Many extends Relationship { { if ($this->model->timestamps()) { - $attributes['updated_at'] = $this->model->get_timestamp(); + $attributes['updated_at'] = new \DateTime; } return $this->table->update($attributes); diff --git a/laravel/database/query/grammars/grammar.php b/laravel/database/query/grammars/grammar.php index 50d2b207..acc77891 100644 --- a/laravel/database/query/grammars/grammar.php +++ b/laravel/database/query/grammars/grammar.php @@ -5,6 +5,13 @@ use Laravel\Database\Expression; 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. * diff --git a/laravel/database/query/grammars/sqlserver.php b/laravel/database/query/grammars/sqlserver.php index 8edb30c0..def519a5 100644 --- a/laravel/database/query/grammars/sqlserver.php +++ b/laravel/database/query/grammars/sqlserver.php @@ -11,6 +11,13 @@ class SQLServer extends Grammar { */ 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. * diff --git a/laravel/database/schema/grammars/postgres.php b/laravel/database/schema/grammars/postgres.php index 760af1a8..e0492ba7 100644 --- a/laravel/database/schema/grammars/postgres.php +++ b/laravel/database/schema/grammars/postgres.php @@ -368,7 +368,7 @@ class Postgres extends Grammar { */ protected function type_date(Fluent $column) { - return 'TIMESTAMP'; + return 'TIMESTAMP(0) WITHOUT TIME ZONE'; } /**