In my application, there is are projects, tags, and profiles (users). 
Projects can be tagged by multiple profiles. Each tag has a many-to-many 
relationship both projects and profiles. There's a mapping table 
(tag_project_profile_map) with a 3-column primary key that relates those 
three tables.

I've defined classes for Tag, Project and Profile, and also a mapping 
class for TagProjectProfileMap. These are all spelled out in more detail 
at the bottom of the message.

I want to add an tag entry to a project, specifying both the name of the 
tag, and the user who created it. I can do this:

my $project = Project->new( id => 1);
$project->load;

$project->tags( { name = 'cool_stuff' } );
$project->save;

but it sets the profile_id to 0 in the mapping table. Not surprising, 
but not what I want.

I'd like to be able to add tags to a project as follows (specifying the 
missing primary key value):

$project->tags( { name = 'cool_stuff', profile_id => 1 } );
$project->save;

However, this gives me an error because the 'tags' table doesn't 
actually have a profile_id, only the mapping table does.

I looked at trying define a custom primary_key_generator() function in 
my TagProjectProfileMap, but I didn't see that I could get the 
additional parameters.

Is there some magic I can use to make this happen, or can I only create 
entries in the mapping table directly:
my $mapping = TagProjectProfileMap->new(
        {project_id => 1,
          tag_id => 2,
          profile_id => 3});
$mapping->save;


Many thanks,

- jud




Here are the details of my db and classes:

DB TABLE SETUP
===================


table projects (
        id int not null primary key,
        name varchar(255) NOT NULL ,
);

table tags (
        id int not null primary key
        name varchar(255) NOT NULL,
        INDEX (name)
);

table profiles (
        id int not null primary key
        username        varchar(255) NOT NULL
        INDEX (username)
);

table tag_project_profile_map (
        tag_id int not null references tags (id),
        profile_id int not null references profiles (id),
        project_id int not null references project (id),
PRIMARY KEY (tag_id, project_id, profile_id )
);


Rose::DB::Object classes
=========================

package Tag;
use base 'My::Rose::DB::Object';
__PACKAGE___->meta->setup(
        table => 'tags',
        ...
        relationships => [
                profiles => {
                        type => 'many to many',
                        map_class => 'TagProjectProfileMap',
                         map_from => 'tag',
                        map_to => 'profile',
                },
                projects => {
                        type => 'many to many',
                        map_class => 'TagProjectProfileMap',
                         map_from => 'tag',
                        map_to => 'projects',
                },
        ],
);



package Project;
use base 'My::Rose::DB::Object';
__PACKAGE___->meta->setup(
        table => 'profiles',
        ...
        relationships => [
                tags => {
                        type => 'many to many',
                        map_class => 'TagProjectProfileMap',
                         map_to => 'tag',
                        map_from => 'profile',
                },
        ],
);


package TagProjectProfileMap;
use base My::Rose::DB::Object;
__PACKAGE__->meta->setup(
        table => 'tag_project_profile_map',
        ...
        primary_keys = [ qw(tag_id project_id profile_id) ],
        foreign_keys => [ qw( project_id profile_id tag_id ) ],
        ],
);



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to