Re: linking on differently-named foreign keys

[prev] [thread] [next] [Date index for 2005/02/14]

From: Ofer Nave
Subject: Re: linking on differently-named foreign keys
Date: 18:44 on 14 Feb 2005
Tony Bowden wrote:

>On Mon, Feb 14, 2005 at 12:58:59AM -0800, Ofer Nave wrote:
>  
>
>>Now, what about the Message class?  When I set up the two has_a 
>>relationships, does has_a also take a third argument so I can tell it to 
>>join to account_id and not sender_id/receiver_id, like this:
>>Message->has_a( sender => "Account", account_id );
>>Message->has_a( receiver => "Account", account_id );
>>    
>>
>
>You don't need it:
>
>Message->has_a( sender => "Account" );
>Message->has_a( receiver => "Account" );
>
>  
>
>>Or does it figure it out from the third argument in has_many in the 
>>Account class?  (I just reread the docs on has_a twice to make sure I 
>>didn't miss an answer.)
>>    
>>
>
>There's no figuring out to be done. There's no ambiguity. All it does is
>inflate the value to be an object of the given class. You could have 492
>columns all inflating to the same class without any problem.
>
>Tony
>  
>
The lightbulb is still off.  Just to recap, here's where we're at currently:

---
01:# CREATE TABLE account ( account_id INT );
02:# CREATE TABLE message ( sender_id INT, receiver_id INT );
03:
04:package Account;
05:use base 'Class::DBI';
06:Account->table( 'account' );
07:Account->columns->( Primary => qw/ account_id / );
08:Account->has_many( inbox  => 'Message', 'receiver_id );
09:Account->has_many( outbox => 'Message', 'sender_id   );
10:
11:package Message;
12:use base 'Class::DBI';
13:Message->table( 'message' );
14:Message->columns->( Primary => qw/ sender_id receiver_id / );
15:Message->has_a( sender   => 'Account' );
16:Message->has_a( receiver => 'Account' );
---

So the one thing I still don't get is, on lines 15-16, how does the 
Message class figure out that it's the 'sender_id' field that needs to 
be inflated into an object of the Account class when the sender() method 
is called?  Is it because the Account class already knows about it's 
side of the one-to-many relationship, and the Message class uses the 
Account class's defined relationships to work out it's end of the deal?

-ofer

(message missing)

linking on differently-named foreign keys
Ofer Nave 08:38 on 14 Feb 2005

Re: linking on differently-named foreign keys
Tony Bowden 08:47 on 14 Feb 2005

Re: linking on differently-named foreign keys
Tony Bowden 09:50 on 14 Feb 2005

Re: linking on differently-named foreign keys
Ofer Nave 18:44 on 14 Feb 2005

Re: linking on differently-named foreign keys
Edward J. Sabol 20:00 on 14 Feb 2005

Generated at 17:31 on 15 Feb 2005 by mariachi v0.52