Skip to content

Instantly share code, notes, and snippets.

@s-aska s-aska/Model.pm
Created Feb 28, 2011

Embed
What would you like to do?
reconnect and datetime auto Model.pm
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
You can’t perform that action at this time.