[Dbix-class] Pointless update on a date field
Jason Kohles
email at jasonkohles.com
Sat Sep 15 16:47:28 GMT 2007
On Sep 13, 2007, at 3:01 PM, Richard Jones wrote:
> Jonathan Rockway wrote:
>> Richard Jones wrote:
>>> There is obviously something amiss because if I omit my own
>>> date_to_mysql() method then system tries to retrieve dates in
>>> dd/mm/yyyy format.
>> I think you're misunderstanding InflateColumn::DateTime.
>
> Well you're not wrong there.
>
> Here's some
>> example code:
>> $some_rs->create({ creation_time => DateTime->now });
>
> But I still don't see how to translate that to my example. Do I
> need to setup __PACKAGE__->inflate_columns() in Schema::Foo as in
> the docs? I assumed so and did so, using DateTime::Format::MySQL-
> >parse_datetime() for inflation and D::F::M::format_datetime() for
> deflation.
>
No, all you have to do is:
__PACKAGE__->load_components( qw( InflateColumn::DateTime Core ) );
and then...
__PACKAGE__->add_columns(
dob => { data_type => 'date' },
);
InflateColumn::DateTime overloads register_column, so when you add a
column with a data_type of 'datetime' or that starts with
'timestamp', it sets up the inflator/deflator for you (check the
overloaded register_column() method in InflateColumn::DateTime to see
exactly what it is doing.)
> How then do I do something like $schema->update( \%form_data ) when
> the user submits a date formatted as '20/12/2000'. How would you do
> it with the US equivalent '12/20/2000'?
>
As I understand it, you have a date_to_mysql() method that takes a
variety of different date formats and turns them into mysql formatted
dates, what you should probably do is turn that into a
date_to_datetime() method, that returns a DateTime object instead of
a mysql-formatted date. In my case I just use HTML::FormFu with an
inflator that turns dates from the form into DateTime objects
automatically. If you really want to update your database with
'12/20/2000', one possible way to do that would be...
use DateTime::Format::Strptime qw( strptime );
$some_dbic_object->updated_time( strptime( '%m/%d/%y', '12/20/2000' ) );
$some_dbic_object->update();
> Actually I think I'm partly there as the dumped sql now shows a
> definite conversion to MySQL format, at least for some dates
> (others are generating Template::Context errors).
>
> Perhaps you could just confirm I'm on the right track with just
> defining the inflate_columns inflate and deflate subs, and letting
> the db do the rest? Thanks.
It's the right track if you are not using InflateColumn::DateTime, if
you are using it, then you don't need to define the inflator/deflator
yourself, you just need to use the plugin (and make sure it appears
in the load_components list *before* Core, as it overloads Core
methods) and then define columns with appropriate data_types.
--
Jason Kohles
email at jasonkohles.com
http://www.jasonkohles.com/
"A witty saying proves nothing." -- Voltaire
More information about the DBIx-Class
mailing list