in reply to Re: Date conversion with Class::DBI
in thread Date conversion with Class::DBI

That is a good explaination of what is going on with with the has_a relationships. I agree with you that the documentation for Class::DBI in regards to this is not that great, and it took me a while to figure things out as well.

I have a couple of small additions to make to clarify or simplify your example.

__PACKAGE__->has_a( startdate => 'Date::Class', inflate => sub { Date::Class->new(shift) }, deflate => 'some_method_name' );

That code can be reduced to:

__PACKAGE__->has_a( startdate => 'Date::Class', inflate => 'new', deflate => 'some_method_name' );

Since by default Class::DBI will call the inflate method as a class method and pass it the value as the first arguement. It can even be reduced to:

__PACKAGE__->has_a( startdate => 'Date::Class', deflate => 'some_method_name' );

Since 'new' is the default method used for inflation.

Similarly, for the deflate method, stringification is used by default. So if you don't provide a 'deflate' method, then Class::DBI will assume that your object overrides "" and will stringify it to deflate it.

Also, a small correction, you mention that the following is incorrect:

SomeTab->create({startdate => '07/08/03'});

That will actually work, since Class::DBI is smart enough to call the 'inflate' method when you pass it a string. I will agree that it is better in most cases to pass an object, but a string will work just as well. The only caveat is that the string you pass must be consistent with what 'deflate' actually generates.

So in other words, you can pass it a 'deflated' string or an 'inflated' object and Class::DBI will do the right thing.

There seems to be enough information in this thread to get a good start on a Tutorial on relationships in Class::DBI...

Cheers,

Cees