[Dbix-class] many_to_many data access

Patrick Meidl patrick at pantheon.at
Thu Jul 12 08:29:55 GMT 2012


On Thu, Jul 12 2012, Robyn Jonahs <learn.catalyst at gmail.com> wrote:

> The cities, states and country relationships are all many_to_many so
> once I have one, I can do the others.

as I mentioned before, in terms of a clean database design, shouln't
your relationships look like this: City belongs_to a State, which in
turn belongs_to a Country. having city, state and country as independent
many_to_many on the store violates the true relationships between these
entities.

if you implement it like this, you would end up with code like this:

foreach my $city ($store->cities) {
  print "City: " . $city->city . "\n";
  print "State: "  . $city->state->name . "\n";
  print "Country: "  . $city->state->country->name . "\n";
}

you could also use the proxy attribute on your belongs_to relations to
shortcut these calls to something like $city->state_name and
$city->country_name.

HTH

    patrick

-- 
Patrick Meidl ........................ patrick at pantheon.at
Vienna, Austria ...................... http://gplus.to/pmeidl




More information about the DBIx-Class mailing list