[Dbix-class] has_one joining through another table

Matt S Trout dbix-class at trout.me.uk
Tue Jun 26 20:58:04 GMT 2007


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 = 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 picture
and owner objects, and then have the UserPicture object proxy all the picture
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 will
work normally.

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director    Want a managed development or deployment platform?
 Shadowcat Systems Ltd.  Contact mst (at) shadowcatsystems.co.uk for a quote
http://chainsawblues.vox.com/             http://www.shadowcatsystems.co.uk/ 



More information about the Dbix-class mailing list