Re: Performing a join...?

[prev] [thread] [next] [Date index for 2004/12/16]

From: Matt S Trout
Subject: Re: Performing a join...?
Date: 16:46 on 16 Dec 2004
On Thu, Dec 16, 2004 at 11:35:53AM -0500, Kingsley Kerce wrote:
> CDBI can help you to model the many-to-many relationship between users
> and modules:
> 
> UserModule->has_a(user_id => 'User');
> UserModule->has_a(module_id => 'Module);
> User->has_many(modules => [ 'UserModule' => 'module_id' ]);
> Module->has_many(users => [ 'UserModule' => 'user_id' ]);
> 
> Now you can write things like:
> 
> my $user = ...
> foreach my $module ($user->modules) {
>   print $module->description;
>   ...
> }
> 
> my $module = ...
> foreach my $user ($module->users) {
>   print $user->company;
>   ...
> }
> 
> $user->add_to_modules({
>   module_id => module->id,
> });
> 
> That last bit will give a user a new module.
> 
> In order to involve the country and paf attributes, you'll either have
> to write something similar to your find_modules() (though perhaps
> simplified by the methods that CDBI set up for you), or -- more
> efficiently -- drop down to the SQL level with CDBI's set_sql() and
> run the join that way.
> 
> I can't see a clever way to dynamically include module attributes
> using set_sql, but perhaps someone else can suggest that.

Have a look at http://www.class-dbi.com/cgi-bin/wiki/index.cgi?UsingJoins
for a useful start.

        -- 
        Bring me my etherkiller; Oh clouds unfold! / Bring me the magic smoke of desire
I shall not cease from mental fight / Nor shall my LART rest in my hand
Till we have buried the bodies / Of all the lusers in all this land
  -- rpg, ASR                        [ My homepage is http://www.trout.me.uk/ ]

Performing a join...?
jay 15:39 on 16 Dec 2004

Re: Performing a join...?
Kingsley Kerce 16:35 on 16 Dec 2004

Re: Performing a join...?
Matt S Trout 16:46 on 16 Dec 2004

Re: Re: Performing a join...?
jay 17:25 on 16 Dec 2004

Generated at 09:04 on 20 Dec 2004 by mariachi v0.52