Thank you very much for all of your help (and amazing work on these
classes in general).

Now that I am up to speed on this behaviour, let me run something by
you, and you can tell me if I'm barking up the wrong tree.

What I really want to be able to do is (using an m-to-m's add_on_save)

my $item = My::DBO::Item->new();
$package->add_items($item);    # related by an m-to-m with add_on_save
$item->map_record->notes('save me too'); # does map_record even exist yet?
$package->save;

Basically the map_record class has a couple extra columns that store
important information about the relationship.  Do I have to do this
after the save()? Seems trivial in the above example but these
operations happen in completely separate areas in my code.

Cheers,
Derek



On 4/11/07, John Siracusa <[EMAIL PROTECTED]> wrote:
> On 4/11/07, Derek Watson <[EMAIL PROTECTED]> wrote:
> >     items =>
> >     {
> >       type        => 'many to many',
> >       map_class   => 'My::DBO::PackageItemMap',
> >       map_from    => 'package',
> >       map_to      => 'item',
> >       manager_args=> { with_map_records => 1, },
> >     },
>
> When you specify a m-to-m relationship with a with_map_records manager
> arg, the method used to store map records is created for you at class
> initialization time.  You can specify the name of this method (by
> setting with_map_records to a method name instead of just 1), but the
> default name for this method is "map_record".
>
> When attempting to create this method, if a method of the same name
> already exists, it's assumed that this is a method that you created
> for this purpose, and no further work is needed.  So what happens in
> your case is that the first m-to-m that has with_map_records set
> creates a method named map_record(), and then the second sees the
> previously created map_record() method and does nothing.
>
> This explains the behavior you saw here:
>
> > my $item = My::DBO::Item->new;
> > die $item->map_record; # My::DBO::PackageItemMap=HASH(0xa919514)
> >
> > When I comment out the with_map_records line in PackageItemMap and run
> > the test again:
> >
> > my $item = My::DBO::Item->new;
> > die $item->map_record; # My::DBO::PersonItemMap=HASH(0xa919514)
> >
> > It goes to the next in the chain.  If I comment out all with_map_records,
> >
> > Can't locate object method "map_record" via package "My::DBO::Item" at
> > /usr/lib/perl5/site_perl/5.8.8/Rose/DB/Object.pm line 1500
>
> This situation is corrected in SVN.  Now if you have multiple m-to-m
> relationships with with_map_records set in their relationship
> definitions, all trying to create a map record methods with the same
> name, you'll get an error that looks like this:
>
> "Already made a map record method named map_record in class JCS::B on
> behalf of the relationship 'bs' in class JCS::A.  Please choose
> another name for the map record method for the relationship named 'bs'
> in JCS::C."
>
> (Note that this only applies to map record methods created at class
> setup time in response to m-to-m relationships with with_map_records
> manager_args.  Map record methods used in a Manager calls on a
> per-call basis are exempt from these restrictions.  (But be careful
> with those names too, obviously.))
>
> The second issue:
>
> On 4/10/07, Derek Watson <[EMAIL PROTECTED]> wrote:
> >   my $items= My::DBO::Manager::Item->get_items(
> >     query => [
> >       sku => int($sku),
> >       active => 1,
> >     ],
> >   );
> >
> > But they have map_records that I don't want
> >
> > foreach my $item (@$items) {
> >   die $item->map_record if ($item->map_record);
> > }
>
> is caused by the fact that the auto-created map_record() method will
> create a new object of the appropriate class if no map record exists.
> So this loop:
>
> > foreach my $item (@$items) {
> >   die $item->map_record if ($item->map_record);
> > }
>
> is actually causing new map-class objects to be created each time
> $item->map_record is called inside the "if(...)".  I'm not sure why I
> decided to make the auto-created map_record() method behave this way,
> but all tests seem to pass without this behavior, so I've removed it
> in SVN.
>
> -John
>

-------------------------------------------------------------------------
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