Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/eloquent unix timestamps #769

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 29 additions & 5 deletions laravel/database/eloquent/model.php
Expand Up @@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

most Laravel code use own line for braces*, and condition without semicolon is accepted by in Laravel (since @taylorotwell use it mostly)

  • edited

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semicolon? Braces. (Just trying to avoid confusion :) ).

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.
*
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -789,4 +813,4 @@ public static function __callStatic($method, $parameters)
return call_user_func_array(array(new $model, $method), $parameters);
}

}
}
6 changes: 3 additions & 3 deletions laravel/database/schema/grammars/mysql.php
Expand Up @@ -382,7 +382,7 @@ protected function type_boolean(Fluent $column)
*/
protected function type_date(Fluent $column)
{
return 'DATETIME';
return ($column->unix) ? 'INT UNSIGNED' : 'DATETIME';
}

/**
Expand All @@ -393,7 +393,7 @@ protected function type_date(Fluent $column)
*/
protected function type_timestamp(Fluent $column)
{
return 'TIMESTAMP';
return ($column->unix) ? 'INT UNSIGNED' : 'TIMESTAMP';
}

/**
Expand All @@ -418,4 +418,4 @@ protected function type_blob(Fluent $column)
return 'BLOB';
}

}
}
6 changes: 3 additions & 3 deletions laravel/database/schema/grammars/postgres.php
Expand Up @@ -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';
}

/**
Expand All @@ -379,7 +379,7 @@ protected function type_date(Fluent $column)
*/
protected function type_timestamp(Fluent $column)
{
return 'TIMESTAMP';
return ($column->unix) ? 'INT' : 'TIMESTAMP';
}

/**
Expand All @@ -404,4 +404,4 @@ protected function type_blob(Fluent $column)
return 'BYTEA';
}

}
}
6 changes: 3 additions & 3 deletions laravel/database/schema/grammars/sqlite.php
Expand Up @@ -312,7 +312,7 @@ protected function type_boolean(Fluent $column)
*/
protected function type_date(Fluent $column)
{
return 'DATETIME';
return ($column->unix) ? 'INTEGER' : 'DATETIME';
}

/**
Expand All @@ -323,7 +323,7 @@ protected function type_date(Fluent $column)
*/
protected function type_timestamp(Fluent $column)
{
return 'DATETIME';
return ($column->unix) ? 'INTEGER' : 'DATETIME';
}

/**
Expand All @@ -348,4 +348,4 @@ protected function type_blob(Fluent $column)
return 'BLOB';
}

}
}
6 changes: 3 additions & 3 deletions laravel/database/schema/grammars/sqlserver.php
Expand Up @@ -386,7 +386,7 @@ protected function type_boolean(Fluent $column)
*/
protected function type_date(Fluent $column)
{
return 'DATETIME';
return ($column->unix) ? 'INT' : 'DATETIME';
}

/**
Expand All @@ -397,7 +397,7 @@ protected function type_date(Fluent $column)
*/
protected function type_timestamp(Fluent $column)
{
return 'TIMESTAMP';
return ($column->unix) ? 'INT' : 'TIMESTAMP';
}

/**
Expand All @@ -422,4 +422,4 @@ protected function type_blob(Fluent $column)
return 'VARBINARY(MAX)';
}

}
}
29 changes: 21 additions & 8 deletions laravel/database/schema/table.php
Expand Up @@ -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'));
}

/**
Expand Down Expand Up @@ -412,4 +425,4 @@ protected function column($type, $parameters = array())
return end($this->columns);
}

}
}
18 changes: 17 additions & 1 deletion laravel/documentation/database/eloquent.md
Expand Up @@ -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.

<a name="relationships"></a>
Expand Down Expand Up @@ -498,4 +514,4 @@ Sometimes you may wish to limit the attributes that are included in your model's

public static $hidden = array('password');

}
}
5 changes: 4 additions & 1 deletion laravel/documentation/database/schema.md
Expand Up @@ -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
Expand Down Expand Up @@ -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');
$table->drop_foreign('posts_user_id_foreign');