Browse Source

Add DateTime support to database binding layer.

Signed-off-by: Taylor Otwell <taylorotwell@gmail.com>
Taylor Otwell 12 years ago
parent
commit
94b8582865

+ 13 - 0
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.

+ 2 - 12
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.
 	 *

+ 1 - 1
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'];
 		}

+ 1 - 1
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);

+ 7 - 0
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.
 	 *

+ 7 - 0
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.
 	 *

+ 1 - 1
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';
 	}
 
 	/**