[Dbix-class] Pointless update on a date field
Richard Jones
ra.jones at dpw.clara.co.uk
Mon Sep 17 20:26:05 GMT 2007
Jason Kohles wrote:
>> 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
Right, all appears to work OK if I use a date_to_datetime() method
instead of a date_to_mysql() one. But in one sense I don't really gain
much as I still have to manually parse the users' date input to create a
datetime object, ie
instead of date_to_mysql():
return join '-', Decode_Date_EU($date);
I have:
my ($yr, $month, $day) = Date::Calc::Decode_Date_EU($date);
return DateTime->new( year => $yr, month => $month, day => $day);
Slightly more code, and I still have to send every instance of a users'
date input to the date_to_datetime() method. Is this the price I have to
pay for allowing flexibility in allowing the user to input dates in any
(EU) date format?
I was hoping I could use inflate/deflate to transparently handle dates
going in and out of the db without requiring further manipulation.
DateTime::Format::MySQL doesn't really help as it requires dates in
MySQL format (ie yyyy-mm-dd) already.
One advantage I gain by using a DateTime object though is that the dob
field is handled correctly now, and is not updated inappropriately,
which was the original point of this thread.
--
Richard Jones
More information about the DBIx-Class
mailing list