cascading update

[prev] [thread] [next] [Date index for 2005/04/08]

From: jldommer
Subject: cascading update
Date: 18:08 on 08 Apr 2005
Hi all,

I've been working with cdbi for a couple of months now, but Ive been using a lot of fairly inelegant code to mimic what my boss refers to as 'cascading submit' with has_many.  What he means by 'cascading sumbit' is that if I have a parent object with multiple children and I change some of the fields in the child objects, I'd like to be able to send these changes to the db by calling update on the parent so that I don't have to call update on each of the individual children.  It has to work in the opposite direction as well (i.e. if I have a child object and change a field in the parent object, I'd like to update the child instead of explicitly generating the parent to call update on, which is what I do now).  Here is an example of what I'd like to have work:

I have a person (parent) object and a cat (child) object related like this:

TestOracle::Cat->has_a ( 'pid' => 'TestDBIsA::Person' );
TestOracle::Person->has_many( 'cats' , 'TestDBIsA::Cat' => 'pid' );

If I have retrieved my person from the database, I would like to be able to update the cat's name like this:
my @cats = $person->cats;
my $cat = $cats[0];
$cat->name('Mr. Whiskers');

$person->update; # and I would like this to save the changes to cat.

the call to update on $person does not save the change to the cat.  This isn't really a big issue going in this direction, since I can loop over the array and call update every time (my current mimic), but on larger arrays I'd like to just update once.  It becomes an bigger issue when I try this:

my $cat = TestOracle::Cat->retrieve(1);
$cat->pid->firstname('Mike');
$cat->update;

Note that firstname is a column in my person class, not in cat.  pid is the foreign key in cat to person.  The above code doesn't work, and instead I have to explicitly create a person object to save the changes that were made, which defeats the purpose of making the changes to the parent via the child.  I've been searching the list archive, but I haven't found anything that seems to deal with this.  Is there any way that I can do cascading sumbit other than with the workarounds that I'm currently implementing?

Jennifer Dommer

cascading update
jldommer 18:08 on 08 Apr 2005

Re: cascading update
yoorobot 18:29 on 08 Apr 2005

Re: cascading update
jldommer 14:10 on 13 Apr 2005

Re: cascading update
Perrin Harkins 18:41 on 08 Apr 2005

Re: cascading update
Tony Bowden 19:17 on 08 Apr 2005

Generated at 12:49 on 16 Apr 2005 by mariachi v0.52