[Dbix-class] has_one joining through another table

Steve Francia steve at takkle.com
Tue Jun 26 22:31:28 GMT 2007


I don't think we are on the same page. Permit me to clarify.

Allow me to illustrate the tables and relationships:

picture
+ picture_id
+ lots of other fields of no importance here

user_picture
+ user_picture_id
+ fk_picture_id
+ fk_user_id
+ owner (either 1 or 0)

user
+ user_id
+ lots of other fields of no importance here

in User Schema:
__PACKAGE__->has_many(user_pictures =3D>'Schema::UserPicture', 'fk_user_id'=
);
__PACKAGE__->many_to_many(pictures =3D> 'user_pictures', 'picture');

in UserPicture Schema:
__PACKAGE__->belongs_to(user =3D> 'Schema::User', 'fk_user_id');
__PACKAGE__->belongs_to(picture =3D> 'Schema::Picture', 'fk_picture_id');

in Picture Schema:
__PACKAGE__->has_many(user_pictures =3D>'Schema::UserPicture',
'fk_picture_id');
__PACKAGE__->many_to_many(users =3D> 'user_pictures', 'user');

This is all working as designed.
$user->pictures and $picture->users both work properly. (Yes, a picture
can have multiple users)

What isn't working right now is:
I would like to have a special relationship for the specific user who
happens to also be the owner. This relationship would exist at
$picture->owner . Ideally it would be in addition to the relationship
already existing between a picture and it's users as the owner would be
a part of that set as well.

I kinda see how proxy could fit that need, but some of the other answers
you provided made me think I didn't explain myself properly the first time.
I though a has_one would do it, but more and more that doesn't seem to
be the case.
-Steve

Matt S Trout wrote:
> On Tue, Jun 26, 2007 at 01:14:17PM -0400, Steve Francia wrote:
>   =

>> In my database I have three tables user, picture and user_picture.
>> They are setup with many to many relationships and work great.
>>
>> The user_picture table also has an extra field, owner (bit), which
>> designates the original uploader.
>>
>> What I would like to have is a has_one relationship that joins picture
>> to user through the user_picture table where owner =3D 1.
>>     =

>
> It's a foreign key, so it's a belongs_to, not a has_one.
>
>   =

>> so $video->owner would be the user object.
>>
>> In that way it would work similarly to the many_to_many convenience
>> relationships.
>>     =

>
> For this, I'd has_many across to user_picture, have it prefetch the pictu=
re
> and owner objects, and then have the UserPicture object proxy all the pic=
ture
> methods (there's a rel attr to do this for you). Then you can treat the
> UserPicture object as a Picture object for most purposes but $up->owner w=
ill
> work normally.
>
>   =


-- =

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/20070626/099=
83155/attachment.htm


More information about the Dbix-class mailing list