On 10/1/06 7:13 AM, Ask Bjørn Hansen wrote:
> With RDBO we've so far just used "TIMESTAMP" columns, but now we have
> a table where we need to keep track of both when the entry was
> created and when it was last updated.   MySQL can't do that
> automatically, so now I'd like to make RDBO do it.
> 
> What'd be the best way?

One way is to override save() and set the last_modified column to the
current time before calling the super-class save:

  sub save
  {
    my($self) = shift;
    $self->last_modified('now');
    return $self->SUPER::save(@_);
  }

Another alternative is to use an on_save* trigger for the last_modified
column:

    __PACKAGE__->meta->setup
    (
      ...
      columns => 
      [
        ...
        last_modified =>
        {
          type => 'timestamp',
          on_save => sub { shift->last_modified('now') },
        },
        ...
      ],
      ...
    );

Yet another alternative is to set the default value for the column to "now"
and then add an on_load trigger that sets the column to undef, causing the
default value to be used the next time it's saved.

There are probably other possibilities too.  Which is "best" depends on
which one you think is the most clear.  I think it's probably a tie between
the on_save trigger and overriding save().  Both make it pretty clear what's
going on.

-John

* Note that there's a typo in the on_save POD.  It says "on_load" instead of
"on_save" in the trigger description.  That will be fixed in the next
release.



-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to