[Dbix-class] Multiple belongs_to relationships referencing same class

Tobias Kremer list at funkreich.de
Wed Apr 25 21:58:24 GMT 2007


Am 25.04.2007 um 22:42 schrieb Brandon Black:

> On 4/25/07, Tobias Kremer <list at funkreich.de> wrote:
>> Hi folks,
>>
>> I have a table which has two columns referencing the same class,  
>> something
>> like this:
>
> You don't give a lot of details on what the failing behavior actually
> is, but I'm guessing that at least one problem you're having is that
> you're proxying the same column names into the local class twice from
> two different relationships.  This might be a good place to not use
> proxy, considering the conflict.

Sounds like my kind of problem - what are the alternatives to proxy?
Consider the example I posted to the list earlier which should make my
intentions clearer:

I have a table storing user-friendship associations (UserFriendship  
class):

id, user_id, friend_user_id, date_added, date_confirmed

If a user adds another user as a friend, an entry to the table is  
inserted
with date_added = NOW() and date_confirmed = NULL. If the other user  
confirms the
friendship request, date_confirmed is set to NOW().

Now, I'd like to have three methods defined in the User class:

1) confirmed_friendships() - User objects which are friends of $user
    (I suppose, this needs a UNION but I have absolutely no clue how  
to pull this off yet)
2) unconfirmed_friendships() - User objects which $user added as a  
friend but aren't confirmed yet
3) friendship_requests() - User objects which requested to add $user  
as friend

All three methods are really doing ->search on the UserFriendship  
Class but are
supposed to return User objects for either user_id or friend_user_id  
depending on
the purpose.

Any ideas on how to implement this are highly appreciated because  
this really
is trivial SQL stuff, but getting it to work with the DBIx::Class  
logic makes
my brain hurt :)

--Tobias







More information about the Dbix-class mailing list