1 package DBIx::Class::InflateColumn::DateTime;
5 use base qw/DBIx::Class/;
9 DBIx::Class::InflateColumn::DateTime - Auto-create DateTime objects from date and datetime columns.
13 Load this component and then declare one or more
14 columns to be of the datetime, timestamp or date datatype.
17 __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
18 __PACKAGE__->add_columns(
19 starts_when => { data_type => 'datetime' }
22 Then you can treat the specified column as a L<DateTime> object.
24 print "This event starts the month of ".
25 $event->starts_when->month_name();
29 This module figures out the type of DateTime::Format::* class to
30 inflate/deflate with based on the type of DBIx::Class::Storage::DBI::*
31 that you are using. If you switch from one database to a different
32 one your code should continue to work without modification (though note
33 that this feature is new as of 0.07, so it may not be perfect yet - bug
34 reports to the list very much welcome).
38 __PACKAGE__->load_components(qw/InflateColumn/);
40 __PACKAGE__->mk_group_accessors('simple' => '__datetime_parser');
42 =head2 register_column
44 Chains with the L<DBIx::Class::Row/register_column> method, and sets
45 up datetime columns appropriately. This would not normally be
46 directly called by end users.
51 my ($self, $column, $info, @rest) = @_;
52 $self->next::method($column, $info, @rest);
53 return unless defined($info->{data_type});
54 my $type = lc($info->{data_type});
55 $type = 'datetime' if ($type eq 'timestamp');
56 if ($type eq 'datetime' || $type eq 'date') {
57 my ($parse, $format) = ("parse_${type}", "format_${type}");
58 $self->inflate_column(
62 my ($value, $obj) = @_;
63 $obj->_datetime_parser->$parse($value);
66 my ($value, $obj) = @_;
67 $obj->_datetime_parser->$format($value);
74 sub _datetime_parser {
76 if (my $parser = $self->__datetime_parser) {
79 my $parser = $self->result_source->storage->datetime_parser(@_);
80 return $self->__datetime_parser($parser);
90 =item More information about the add_columns method, and column metadata,
91 can be found in the documentation for L<DBIx::Class::ResultSource>.
97 Matt S. Trout <mst@shadowcatsystems.co.uk>
101 Aran Deltac <bluefeet@cpan.org>
105 You may distribute this code under the same terms as Perl itself.