[Dbix-class] Left join "on" conditions

Brian E. Lozier brian at massassi.com
Tue Apr 5 03:40:08 GMT 2011


I've recently begun a small project and decided to try DBIx::Class.  I
am trying to use prefetch to bring in some related data but one of the
joins should have an extra conditional on it.  Here is the search:

    my $topics_rs = $schema->resultset('Topic')->search(
        { forum_id => $forum_id },
        {
            prefetch => 'topic_view'
        },
    );


The Topic.pm has a relationship:

__PACKAGE__->might_have(topic_view =>
'Fan::DB::Schema::Result::TopicView', 'topic_id');

And TopicView.pm has relationships:

__PACKAGE__->belongs_to(post   => 'Fan::DB::Schema::Result::Post',  'post_id');
__PACKAGE__->belongs_to(myuser => 'Fan::DB::Schema::Result::User',  'user_id');

I'm trying to have the LEFT JOIN "on" condition include the user_id,
so it gets something like this:

SELECT * FROM topics LEFT JOIN topic_views ON (topics.topic_id =
topic_views.topic_id AND topic_views.user_id = ?)

The idea is that I'll always get the "topics" information and only get
the "topic_views" information if topic_id matches and user_id matches
whatever I pass in.

Is there a way to run a search query like this?

Thanks,
Brian



More information about the DBIx-Class mailing list