jdtoronto has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed Monks,

I have an application where I am using 'mapping' in Class::DBI, after some help here a couple of days ago I got that working quite happily. But I need to figure out how to add a map when I add a user.

My three classes are these:

package AppSys::MapPrUser; use base AppSys::DBI; __PACKAGE__->table("map_pr_usr"); __PACKAGE__->columns(Primary => qw(provider_id user_id)); __PACKAGE__->has_a(provider_id => AppSys::Provider); __PACKAGE__->has_a(user_id => AppSys::User); package AppSys::User; use base AppSys::DBI; __PACKAGE__->set_up_table("ap_uprofile"); __PACKAGE__->has_many(providers => ['AppSys::MapPrUser' => 'provider_i +d']); __PACKAGE__->has_many(appointments => 'AppSys::PrAppointments', 'useri +d'); package AppSys::Provider; use base AppSys::DBI; __PACKAGE__->set_up_table("ap_prprofile"); __PACKAGE__->has_a(defAptType => 'AppSys::ApptTypes'); __PACKAGE__->has_many(appointments => 'AppSys::PrAppointments', 'provi +derid', {sort => 'appdate'}); __PACKAGE__->has_many(users => ['AppSys::MapPrUser' => 'user_id']); __PACKAGE__->has_many(appointment_types => 'AppSys::ApptTypes', 'provi +derid');
When I add a new 'AppSys::User' I want to also add a mapping in 'AppSys::MapPrUser', I have the appropriate provider id I wish to reference, but I am not sure how to write the method to add it, nor which Class it should actually appear in!

Thanks in advance!
...john

jdtoronto

  • Comment on Class::DBI - addiing map entry when adding to one of the mapped classes.
  • Download Code

Replies are listed 'Best First'.
Re: Class::DBI - addiing map entry when adding to one of the mapped classes.
by cees (Curate) on Feb 17, 2004 at 20:43 UTC

    My guess would be to just call add_to_providers and pass it the provider as a provider_id:

    my $user = AppSys::User->create({ ... }); # Get the AppSys::Provider object my $provider = AppSys::Provider->retrieve(...); $user->add_to_providers({ provider_id => $provider, });

    It looks like the provider has to exist in the ap_prprofile table for the AppSys::User->add_to_providers call to work. In other words, Class::DBI is not creating a new provider for you, it is just linking an existing provider to the user you just created.

    I haven't used the mapping features of Class::DBI myself, but this is what I understand from the doc example.

    - Cees