[Dbix-class] has_one joining through another table

Marc Mims marc at questright.com
Tue Jun 26 23:05:06 GMT 2007


* Steve Francia <steve at takkle.com> [070626 14:40]:
> in User Schema:
> __PACKAGE__->has_many(user_pictures =>'Schema::UserPicture', 'fk_user_id');
> __PACKAGE__->many_to_many(pictures => 'user_pictures', 'picture');
> 
> in UserPicture Schema:
> __PACKAGE__->belongs_to(user => 'Schema::User', 'fk_user_id');
> __PACKAGE__->belongs_to(picture => 'Schema::Picture', 'fk_picture_id');
> 
> in Picture Schema:
> __PACKAGE__->has_many(user_pictures =>'Schema::UserPicture',
> 'fk_picture_id');
> __PACKAGE__->many_to_many(users => '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 would add a user foreign key to picture and drop the owner flag from
user_picture.  The the relationship is straight forward:

In Picture Schema:
__PACKAGE__->belongs_to(owner => 'Schema::User', 'owner_id');

	-Marc



More information about the Dbix-class mailing list