[Dbix-class] user/friendship many-to-many relationship

Tobias Kremer list at funkreich.de
Tue Apr 24 17:32:03 GMT 2007


I'm trying to implement a user/friendship relationship with DBIx::Class. For
this purpose I have two classes/tables:

1) User - stores userdata (id, username etc.)

2) UserFriend - stores user-2-user relationships
   (id, user_id, friend_user_id, date_confirmed etc.)

Now I'd like to call $user->confirmed_friends to get a list of all confirmed
friendships (i.e. date_confirmed is set). The many-to-many relationship
functionality of DBIx does this without problems.
The catch is: A user is considered a friend if it is either stored as user_id
OR friend_user_id (depending on who initiated the request for friendship).
How would one do this with DBIx? The SQL where-condition is supposed to
look something like this:

WHERE ( user_id = $user->id OR friend_user_id = $user->id )
AND date_confirmed IS NOT NULL

Is this possible with DBIx relationships or do I have to create a custom
method which runs a search query returning a ResultSet (maybe with
ResultSetManager)? It works with two independant many-to-many relationships
(e.g. friendships_requested, friendships_accepted) but I'd like to have only
one resultset containing both.

Hope I'm not making things more complicated than they are :)

Thanks!

--Tobias



More information about the Dbix-class mailing list