diff --git a/laravel/database/eloquent/model.php b/laravel/database/eloquent/model.php index 3af49057387..7ebdbea8aca 100644 --- a/laravel/database/eloquent/model.php +++ b/laravel/database/eloquent/model.php @@ -185,11 +185,35 @@ public function fill_raw(array $attributes) */ public static function accessible($attributes = null) { - if (is_null($attributes)) return static::$accessible; + if (is_null($attributes)) { + return static::$accessible; + } static::$accessible = $attributes; } + /** + * Returns the timestamp for the created_at attribute. + * + * @static + * @return \DateTime + */ + public static function created_at() + { + return new \DateTime; + } + + /** + * Returns the timestamp for the updated_at attribute. + * + * @static + * @return \DateTime + */ + public static function updated_at() + { + return new \DateTime; + } + /** * Create a new model and store it in the database. * @@ -218,7 +242,7 @@ public static function update($id, $attributes) { $model = new static(array(), true); - if (static::$timestamps) $attributes['updated_at'] = new \DateTime; + if (static::$timestamps) $attributes['updated_at'] = static::updated_at(); return $model->query()->where($model->key(), '=', $id)->update($attributes); } @@ -447,9 +471,9 @@ public function delete() */ protected function timestamp() { - $this->updated_at = new \DateTime; + $this->updated_at = static::updated_at(); - if ( ! $this->exists) $this->created_at = $this->updated_at; + if (!$this->exists) $this->created_at = static::created_at(); } /** @@ -789,4 +813,4 @@ public static function __callStatic($method, $parameters) return call_user_func_array(array(new $model, $method), $parameters); } -} \ No newline at end of file +} diff --git a/laravel/database/schema/grammars/mysql.php b/laravel/database/schema/grammars/mysql.php index c2ae7455e6d..20a185a752b 100644 --- a/laravel/database/schema/grammars/mysql.php +++ b/laravel/database/schema/grammars/mysql.php @@ -382,7 +382,7 @@ protected function type_boolean(Fluent $column) */ protected function type_date(Fluent $column) { - return 'DATETIME'; + return ($column->unix) ? 'INT UNSIGNED' : 'DATETIME'; } /** @@ -393,7 +393,7 @@ protected function type_date(Fluent $column) */ protected function type_timestamp(Fluent $column) { - return 'TIMESTAMP'; + return ($column->unix) ? 'INT UNSIGNED' : 'TIMESTAMP'; } /** @@ -418,4 +418,4 @@ protected function type_blob(Fluent $column) return 'BLOB'; } -} \ No newline at end of file +} diff --git a/laravel/database/schema/grammars/postgres.php b/laravel/database/schema/grammars/postgres.php index e0492ba7674..9548c577420 100644 --- a/laravel/database/schema/grammars/postgres.php +++ b/laravel/database/schema/grammars/postgres.php @@ -368,7 +368,7 @@ protected function type_boolean(Fluent $column) */ protected function type_date(Fluent $column) { - return 'TIMESTAMP(0) WITHOUT TIME ZONE'; + return ($column->unix) ? 'INT' : 'TIMESTAMP(0) WITHOUT TIME ZONE'; } /** @@ -379,7 +379,7 @@ protected function type_date(Fluent $column) */ protected function type_timestamp(Fluent $column) { - return 'TIMESTAMP'; + return ($column->unix) ? 'INT' : 'TIMESTAMP'; } /** @@ -404,4 +404,4 @@ protected function type_blob(Fluent $column) return 'BYTEA'; } -} \ No newline at end of file +} diff --git a/laravel/database/schema/grammars/sqlite.php b/laravel/database/schema/grammars/sqlite.php index 09102f52ca2..6438254320b 100644 --- a/laravel/database/schema/grammars/sqlite.php +++ b/laravel/database/schema/grammars/sqlite.php @@ -312,7 +312,7 @@ protected function type_boolean(Fluent $column) */ protected function type_date(Fluent $column) { - return 'DATETIME'; + return ($column->unix) ? 'INTEGER' : 'DATETIME'; } /** @@ -323,7 +323,7 @@ protected function type_date(Fluent $column) */ protected function type_timestamp(Fluent $column) { - return 'DATETIME'; + return ($column->unix) ? 'INTEGER' : 'DATETIME'; } /** @@ -348,4 +348,4 @@ protected function type_blob(Fluent $column) return 'BLOB'; } -} \ No newline at end of file +} diff --git a/laravel/database/schema/grammars/sqlserver.php b/laravel/database/schema/grammars/sqlserver.php index 5f756c2e85b..eb8fa2c3a12 100644 --- a/laravel/database/schema/grammars/sqlserver.php +++ b/laravel/database/schema/grammars/sqlserver.php @@ -386,7 +386,7 @@ protected function type_boolean(Fluent $column) */ protected function type_date(Fluent $column) { - return 'DATETIME'; + return ($column->unix) ? 'INT' : 'DATETIME'; } /** @@ -397,7 +397,7 @@ protected function type_date(Fluent $column) */ protected function type_timestamp(Fluent $column) { - return 'TIMESTAMP'; + return ($column->unix) ? 'INT' : 'TIMESTAMP'; } /** @@ -422,4 +422,4 @@ protected function type_blob(Fluent $column) return 'VARBINARY(MAX)'; } -} \ No newline at end of file +} diff --git a/laravel/database/schema/table.php b/laravel/database/schema/table.php index 9518d6e7790..25f81eb09ac 100644 --- a/laravel/database/schema/table.php +++ b/laravel/database/schema/table.php @@ -303,35 +303,48 @@ public function boolean($name) /** * Create date-time columns for creation and update timestamps. * + * @param bool $unix use unix timestamps * @return void */ - public function timestamps() + public function timestamps($unix = false) { - $this->date('created_at'); + $this->date('created_at', $unix); - $this->date('updated_at'); + $this->date('updated_at', $unix); + } + + /** + * Create unix_timestamp columns for creation and update timestamps. + * + * @return void + */ + public function unix_timestamps() + { + $this->timestamps(true); } /** * Add a date-time column to the table. * * @param string $name + * @param bool $unix * @return Fluent */ - public function date($name) + public function date($name, $unix = false) { - return $this->column(__FUNCTION__, compact('name')); + return $this->column(__FUNCTION__, compact('name', 'unix')); } /** * Add a timestamp column to the table. * * @param string $name + * @param bool $unix * @return Fluent */ - public function timestamp($name) + public function timestamp($name, $unix = false) { - return $this->column(__FUNCTION__, compact('name')); + return $this->column(__FUNCTION__, compact('name', 'unix')); } /** @@ -412,4 +425,4 @@ protected function column($type, $parameters = array()) return end($this->columns); } -} \ No newline at end of file +} diff --git a/laravel/documentation/database/eloquent.md b/laravel/documentation/database/eloquent.md index 0706b7d92bd..1bd9a214404 100644 --- a/laravel/documentation/database/eloquent.md +++ b/laravel/documentation/database/eloquent.md @@ -131,6 +131,22 @@ Need to maintain creation and update timestamps on your database records? With E Next, add **created_at** and **updated_at** date columns to your table. Now, whenever you save the model, the creation and update timestamps will be set automatically. You're welcome. +By default Eloquent uses `DateTime` to generate the timestamps. What if you want to use UNIX timestamps? Add the following static methods to your model: + + class User extends Eloquent { + + public static $timestamps = true; + + public static function created_at() { + return time(); + } + + public static function updated_at() { + return time(); + } + + } + > **Note:** You can change the default timezone of your application in the **application/config/application.php** file. @@ -498,4 +514,4 @@ Sometimes you may wish to limit the attributes that are included in your model's public static $hidden = array('password'); - } \ No newline at end of file + } diff --git a/laravel/documentation/database/schema.md b/laravel/documentation/database/schema.md index 16ed318cabe..ea82e82236a 100644 --- a/laravel/documentation/database/schema.md +++ b/laravel/documentation/database/schema.md @@ -64,8 +64,11 @@ Command | Description `$table->float('amount');` | FLOAT equivalent to the table `$table->boolean('confirmed');` | BOOLEAN equivalent to the table `$table->date('created_at');` | DATE equivalent to the table +`$table->date('created_at', true);` | INTEGER equivalent to the table `$table->timestamp('added_on');` | TIMESTAMP equivalent to the table +`$table->timestamp('added_on', true);` | INTEGER equivalent to the table `$table->timestamps();` | Adds **created\_at** and **updated\_at** columns +`$table->unix_timestamps();` | Adds **created\_at** and **updated\_at** INTEGER columns `$table->text('description');` | TEXT equivalent to the table `$table->blob('data');` | BLOB equivalent to the table `->nullable()` | Designate that the column allows NULL values @@ -142,4 +145,4 @@ You may also specify options for the "on delete" and "on update" actions of the You may also easily drop a foreign key constraint. The default foreign key names follow the [same convention](#dropping-indexes) as the other indexes created by the Schema builder. Here's an example: - $table->drop_foreign('posts_user_id_foreign'); \ No newline at end of file + $table->drop_foreign('posts_user_id_foreign');