Add DateTime support to database binding layer.
Signed-off-by: Taylor Otwell <taylorotwell@gmail.com>
This commit is contained in:
parent
7d5b6b3748
commit
94b8582865
|
@ -209,6 +209,19 @@ protected function execute($sql, $bindings = array())
|
||||||
|
|
||||||
$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 @@ public static function update($id, $attributes)
|
||||||
{
|
{
|
||||||
$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 @@ public function delete()
|
||||||
*/
|
*/
|
||||||
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 @@ protected function insert_joining($attributes)
|
||||||
{
|
{
|
||||||
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 @@ public function update(array $attributes)
|
||||||
{
|
{
|
||||||
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 @@
|
||||||
|
|
||||||
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 {
|
||||||
*/
|
*/
|
||||||
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 @@ protected function type_boolean(Fluent $column)
|
||||||
*/
|
*/
|
||||||
protected function type_date(Fluent $column)
|
protected function type_date(Fluent $column)
|
||||||
{
|
{
|
||||||
return 'TIMESTAMP';
|
return 'TIMESTAMP(0) WITHOUT TIME ZONE';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue