views:

31

answers:

1

I have 2 tables that are 1-to-[0/1]. Is there a way to auto create the relationship object/row using Rose::DB::Object:

For example:

# detailed_summary is the 1-to-1 relationship
# if detailed_summary exist, get it
# if not, create a new one with links?
$obj->detailed_summary

Maybe a trigger?

+1  A: 

A column trigger is not what you want. One way to accomplish your goal would be to name your relationships with leading underscores and then write your own underscore-less methods to do the "make one if it doesn't yet exist" thing:

sub detailed_summary
{
  my($self) = shift;

  my $existing_object = $self->_detailed_summary(@_);

  unless($existing_object)
  {
    # Create a new object
    my $new_object = My::Summary->new(...);

    # Assign it to its parent so it will be stored in the
    # database when the parent is save()d, then return it.
    return $self->_detailed_summary($new_object);
  }

  return $existing_object;
}

You could also do the same thing by wrapping the generated detailed_summary() method after it's created, either manually (using typeglobs and subroutine references), or with a CPAN module that can wrap existing subroutines.

(The code above is quite regular and you should be able to automate its creation if you end up doing this a lot.)

John Siracusa
thx, i had some way of doing it that was similar, but wasnt sure if it got cached the same way.. urs is much clearer, thx!
Timmy