I'm writing a Catalyst app using DBIx::Class accessing MySQL for my model. Two of my tables have a many-to-many relationship, and I'm having trouble constructing the right query for the problem at hand.

Okay, so I have the classic many-to-many relationship: an articles table, a tags table, and a links table with article and tag IDs. I'm using DBIx::Class to access these tables, and I have the relationships set up the way you would expect:

schema::articles->has_many(taglinks => 'schema::links', 'article'); schema::links->belongs_to(article => 'schema::articles'); schema::links->belongs_to(tag => 'schema::tags'); schema::tags->has_many(articlelinks => 'links', 'tag'); schema::articles->many_to_many(tags => taglinks => 'tag'); schema::tags->many_to_many(articles => articlelinks => 'article');

Now, at one point in my code I have two arrays with rows from the tags table. I want to find the articles with all of the tag rows in @with and none of the tag rows in @without. The articles can have tags that aren't in either array. (Oh, and to complicate things further, I may want to exclude articles which don't have certain values in columns of the articles table itself, but I'll burn that bridge when I get to it.) How do I do this?

My searching turned up Many-to-many relationships in databases : SOLVED, but I'm not sure how to apply this to DBIx::Class, or if it's even the right approach for this module. Any help would be appreciated.

Thanks,

=cut
--Brent Dax
There is no sig.


In reply to DBIx::Class and many-to-many searching by BrentDax

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.