[Dbix-class] outer join with explicit criteria rears its ugly head (again)

Jason Gottshall jgottshall at capwiz.com
Thu Oct 9 19:12:57 BST 2008


Given the following tables:

Book
   book_id
   title
   ...

Book_User (existence of record indicates that user has read book)
   book_id
   user_id

For a given user, I want to retrieve a list of *all* books, and flag the 
one's he/she has read. The SQL for this is pretty clear:

SELECT book.*, book_user.*
FROM book
LEFT OUTER JOIN book_user
   ON (    book_user.book_id = book.book_id
       AND book.user_id      = 'givenuser'
   )

And I can display a flag whenever book_user.user_id is not null. But I 
can't seem to figure out a clean way of doing this in DBIC. The join 
criteria is dynamic, so it can't be hard-coded into the result source.

So far my solution has been to do two separate queries, selecting 
everything from Book, and selecting from Book_User for the given user. I 
build a quick little hash from the book_user results and just check the 
hash as I loop over the books. It certainly works, but I'm wondering if 
anybody's figured out a way to do this type of query more directly in DBIC.




More information about the DBIx-Class mailing list