[Dbix-class] How to setup a complicated (bilateral) relationship?

Steve Francia steve at takkle.com
Fri Jun 22 18:09:15 GMT 2007


I have a rather complicated relationship that I am struggling with
setting up, hopefully I can explain it so it makes sense. I will also
include what we have thus far.

I have two tables user and friends, friends is a join table that joins
users to users. In my system, friendship is bilateral, meaning to have a
friend is to be one.
I worked through the reciprocal relationship alright, but the bilateral
part has me stumped.

Here are the relevant parts of my two schema files:
User.pm

    __PACKAGE__->has_many(friend =3D> 'Takkle::Schema::Friend', 'fk_user_id=
');
    __PACKAGE__->has_many(user =3D> 'Takkle::Schema::Friend',
    'fk_user_id_friend');
    __PACKAGE__->many_to_many(user =3D> 'friend', 'user');

Friend.pm

    __PACKAGE__->table("friend");
    __PACKAGE__->add_columns(
      "friend_id",
      { data_type =3D> "INT", default_value =3D> undef, is_nullable =3D> 0,
    size =3D> 11 },
      "fk_user_id",
      { data_type =3D> "INT", default_value =3D> undef, is_nullable =3D> 1,
    size =3D> 11 },
      "fk_user_id_friend",
      { data_type =3D> "INT", default_value =3D> undef, is_nullable =3D> 1,
    size =3D> 11 },
      "message",
      {
        data_type =3D> "TEXT",
        default_value =3D> undef,
        is_nullable =3D> 1,
        size =3D> 65535,
      },
      "status",
      { data_type =3D> "TINYINT", default_value =3D> undef, is_nullable =3D>
    1, size =3D> 1 },
      "cr_date",
      {
        data_type =3D> "DATETIME",
        default_value =3D> undef,
        is_nullable =3D> 1,
        size =3D> 19,
      },
      "apr_date",
      {
        data_type =3D> "DATETIME",
        default_value =3D> undef,
        is_nullable =3D> 1,
        size =3D> 19,
      },
    );
    __PACKAGE__->set_primary_key("friend_id");

    __PACKAGE__->belongs_to(user =3D> 'Takkle::Schema::User', 'fk_user_id');
    __PACKAGE__->belongs_to(user =3D> 'Takkle::Schema::User',
    'fk_user_id_friend');

So a users friends are either the fk_user_id where fk_user_id_friend =3D
$user_id or fk_user_id_friend where fk_user_id =3D $user_id...
As you can see, currently I have only implemented the left half of this.

Any Ideas?

-- =

Steve Francia
Lead Developer
Takkle, Inc.
212.792.5859 (fax)
steve at takkle.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20070622/99f=
8276c/attachment.htm


More information about the Dbix-class mailing list