[Dbix-class] Filtering with Many-to-Many relationship

Wallace Reis reis.wallace at gmail.com
Sat Sep 19 02:35:28 GMT 2009


On Fri, Sep 18, 2009 at 3:06 PM, Shawn Marincas
<shawngmarincas at gmail.com> wrote:
> I have a table Products and a table Tags with a many_to_many relationship
> setup, the purpose of which would be to easily generate a resultset of
> products with the given tag(s).  However, I'm running in to problems when I
> want to generate a resultset of product records filtered with multiple
> tags.  I was thinking that one of the following would work:
>
> my $tag1 = 'A';
> my $tag2 = 'B';

my $product_rs = $schema->resultset('Product')->search_rs({
    'tag.tag_id' => { -in => [qw/A B/] }
}, {
    join => { 'product_tags' => 'tag' }
});

> But apparently the many_to_many relationship accessor only returns a list of
> row_objects rather than a resultset itself, so I can't perform resultset
> searches on that.  So I tried the following to chain together two searches:

You've used the ->search method in your code in list context, so it
calls ->all over the resultset and returns a list of row objects.

-- 
     wallace reis/wreis         Catalyst and DBIx::Class consultancy with a clue
     Software Engineer		and a commit bit: http://shadowcat.co.uk/catalyst/
 Shadowcat Systems Limited
 http://www.shadowcat.co.uk     http://www.linkedin.com/in/wallacereis



More information about the DBIx-Class mailing list