On Jan 10, 2008 5:47 AM, Curon Davies <[EMAIL PROTECTED]> wrote: > Thanks for that suggestion, something that I didn't think about. The > add_ prefix doesn't look that nice in the constructor.
Then don't put it in the constructor call :) $product = My::DB::Product->new(name => $^T); $product->add_prices(...); $product->save; > As we would probably need to use it everywhere we create new objects for > inserting into the database, we will probably override the constructor in > our base class to to call the add methods instead of the > 'get_set_on_save' method for both 'one to many' and 'many to many' > relationships. That seems kind of sketchy to me, but here's one way to do it. Recall that the constructor does nothing more than call init() which then calls the methods named by the parameters. You could get the parameters in %args in your own custom new() method, then replace as needed: sub new { my($class, %args) = @_; $args{'add_prices'} = delete $args{'prices'} if($args{'prices'}); return $class->SUPER::new(%args); } Ick (IMO). Do you have any better suggestions? > The only problem is that our code manipulates the object before > deciding to save the data (e.g. does our own additional validation). > With this I can't find any way of getting to the objects that will be > added, as $product->prices returns the empty list. I guess that argues for doing the add_* as a separate step as shown above. my $product = My::DB::Product->new(name => $^T); my @prices; foreach my $price_data ({ ... }, { ... }, ...) { my $price = My::DB::Price->new(%$price_data); # Validate or manipulate $price here ... push(@prices, $price); } $product->add_prices(@prices) if(@prices); $product->save; > I still think the get_set_on_save method should return both what is > already in the database (unless the object isn't in the db, the primary > key isn't set and the column type is serial), along with those that are > going to be in the database when saved. That'd be an unnecessary SELECT for a lot of people. If that's what you want, I think you can ask for it by getting the list first, then adding to it: $product = My::DB::Product->new(...)->load; $product->prices; # loads current prices # or: $product = My::DB::Product->new(...)->load(with => [ 'prices' ]); $product->add_prices(...); # Gets the full list, current prices followed by new prices @prices = $product->prices; -John ------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace _______________________________________________ Rose-db-object mailing list Rose-db-object@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rose-db-object