[Dbix-class] many_to_many data access
Frank Schwach
fs5 at sanger.ac.uk
Thu Jul 12 08:53:30 GMT 2012
On 12/07/12 09:29, Patrick Meidl wrote:
> 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.
except if you are expecting international customers: in many countries
there are no "states", so a city really only "might_have" a state and
the city itself "belongs_to" a country, The (optional) state also
"belongs_to" a country adn there is no need to establish the link
between cities and states at all, not to mention the problem you might
have in some countries where cities can even belong to more than one
province.
Frank
> 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
>
--
The Wellcome Trust Sanger Institute is operated by Genome Research
Limited, a charity registered in England with number 1021457 and a
company registered in England with number 2742969, whose registered
office is 215 Euston Road, London, NW1 2BE.
More information about the DBIx-Class
mailing list