user: id name
item: id name
cart: user item
Tables user and item both have a primary key named id (i recommend you make this an auto incremented unsigned integer). The two fields in the cart table have their _id extensions removed because Class::DBI prefers them that way. They are still foreign keys associated with their respective tables' ids. With all of that out of the way, here is the (untested) code:
package CART::user; use base qw(CART::DBI); __PACKAGE__->table('user'); __PACKAGE__->columns(All => qw(id name)); __PACKAGE__->has_many(items => [qw(CART::item item)]); package CART::item; use base qw(CART::DBI); __PACKAGE__->table('item'); __PACKAGE__->columns(All => qw(id name)); __PACKAGE__->has_many(users => [qw(DVD::user user)]); package CART::cart; use base qw(CART::DBI); __PACKAGE__->table('cart'); __PACKAGE__->columns(Primary => qw/user item/); __PACKAGE__->has_a(user => 'DVD::user'); __PACKAGE__->has_a(item => 'DVD::item'); package main; for my $user (CART::user->retrieve_all) { my $items = $user->items; print $user->name; print " has no items\n" and next unless $items; print "has these items:\n"; print $_->name, "\n" for @$items; }
Better late than never. ;)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to Re: Many-to-many with Class::DBI by jeffa
in thread Many-to-many with Class::DBI by Anonymous Monk

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.