Created
February 28, 2011 04:45
-
-
Save s-aska/846947 to your computer and use it in GitHub Desktop.
reconnect and datetime auto Model.pm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Hamburger::Model; | |
use parent 'Teng'; | |
use DateTime; | |
sub dbh { | |
my $self = shift; | |
$self->SUPER::dbh; | |
$self->connect unless $self->{dbh}; | |
$self->{dbh}; | |
} | |
sub insert { | |
my ($self, $table_name, $args, $prefix) = @_; | |
my $now = DateTime->now( time_zone => 'Asia/Tokyo' ); | |
my $table = $self->schema->get_table( $table_name ); | |
for my $column ( @{$table->columns} ) { | |
$args->{$column} = $now if $column=~/^created_(on|at)$/; | |
} | |
$self->SUPER::insert($table_name, $args, $prefix); | |
} | |
sub update { | |
my ($self, $table_name, $args, $prefix) = @_; | |
my $now = DateTime->now( time_zone => 'Asia/Tokyo' ); | |
my $table = $self->schema->get_table( $table_name ); | |
for my $column ( @{$table->columns} ) { | |
$args->{$column} = $now if $column=~/^modified_(on|at)$/; | |
} | |
$self->SUPER::update($table_name, $args, $prefix); | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment