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


In reply to Re: Re: Date conversion with Class::DBI by cees
in thread Date conversion with Class::DBI by bsb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.