[CDBI] has_a infation of a Time::Piece woes

[prev] [thread] [next] [Date index for 2005/10/24]

From: Peter Speltz
Subject: [CDBI] has_a infation of a Time::Piece woes
Date: 16:31 on 24 Oct 2005
Hi. I'm using 3.09 and Mysql. I had this code for this TIMESTAMP
column named 'finished'.  The column is 0000-00-00 00:00:00 by
default.  If  'finished' has not been set to  a real time, then the
inflation code gives an "Out of Range' Time::Piece error.

__PACKAGE__->has_a("finished" =3D> 'Time::Piece',
        inflate =3D> sub { Time::Piece->strptime(shift, "%Y-%m-%d %T") },
        deflate =3D> sub { my $tp =3D shift; return $tp->ymd.' '.$tp->hms; =
},
);

Anyone know how I would
1) make Time::Piece not have a date IE - stringify to ''.  T::P docs
did not help here.
or
2)change the inflate sub to return undef rather than a Time::Piece
object if the column is not defined yet.

???



For (1) I tried blessing an empty array into Time::Piece. Same out of
range error.

So i tried this for (2)  which from reading the docs , seems it should work=
. :

__PACKAGE__->has_a("finished" =3D> 'Time::Piece',
        inflate =3D> sub {
            my $t =3D shift;
            if ($t =3D~ /^0000-/) {
                  return;  # This makes Time::Piece have current time.
                  # return 0;      # Has beginning of epoch (Jan 1 1970)
            }
            else {
                return Time::Piece->strptime($t, "%Y-%m-%d %T");
            }
        },
      . . .
);


But what is happening is the value my inflate sub returns is getting
passed to the Time::Piece constructor so i am getting some Times i do
not want..

>From has_a section of docs annotated by me [pjs . . . ]
-----------------------------------------------------------------------
If the foreign class is another Class::DBI representation retrieve is
called on that class with the column value.   [ pjs -- Ok. ]  Any
other object will be instantiated either by calling new($value) or
using the given 'inflate' method. [ pjs -- Ok. call constructor with
value of column *OR* use the inflate method ]   If the inflate method
name is a subref, it will be executed, and will be passed the value
and the Class::DBI object as arguments.
[ pjs -- No mention of calling new() with the result of the inflate subref.=
  ]

-----------------------------------------------------------------------

Thanks for any help.

pjs

_______________________________________________
ClassDBI mailing list
ClassDBI@xxxxx.xxxxxxxxxxxxxxxx.xxx
http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi

[CDBI] has_a infation of a Time::Piece woes
Peter Speltz 16:31 on 24 Oct 2005

Re: [CDBI] has_a infation of a Time::Piece woes
Perrin Harkins 16:38 on 24 Oct 2005

Re: [CDBI] has_a infation of a Time::Piece woes
William Ross 16:47 on 24 Oct 2005

Re: [CDBI] has_a infation of a Time::Piece woes
Peter Speltz 17:04 on 24 Oct 2005

Re: [CDBI] has_a infation of a Time::Piece woes
William Ross 18:25 on 24 Oct 2005

Generated at 14:18 on 27 Oct 2005 by mariachi v0.52