On 2/20/06 4:49 AM, Michael Lackhoff wrote:
> I have three tables in a relationship like 'cities', 'states' and
> 'countries'. Now I would like to say something like
> print $city->country->name;

Given classes like this:

    package MyApp::State;
    ...
    __PACKAGE__->meta->relationships
    (
      country => 
      {
        type       => 'many to one',
        class      => 'MyApp::Country',
        column_map => { country_id => 'id' },
      },
    );
    ...

    package MyApp::City;
    ...
    __PACKAGE__->meta->relationships
    (
      state => 
      {
        type       => 'many to one',
        class      => 'MyApp::States',
        column_map => { state_id => 'id' },
      },
    );
    ...

Why not just add a method like this to MyApp::City to get the country for a
city?

    sub country { shift->state->country }

That's probably less typing than an auto-magical multi-level shortcut
relationship would be anyway :)

> p.s.: After some more experimenting I came up with this object
> method:
> sub country {
>     my $self = shift;
>     my $state = MyApp::States->new(id => $self->state_id)->load();
>     my $country = $state->country();
>     return $country;
> }

Since you already have a state() accessor in MyApp::City, there's no reason
to repeat all that relationship "linking" information in the method. (e.g.,
id => $self->state_id)

> This is o.k. for me as a solution but if there is a way to do it the
> "meta->relationship" way I would prefer it because it is more
> readable if everything is in one place.

Compare your p.s. example above with the one-liner I posted.  They both do
basically the same thing, but mine doesn't include any redundant information
about the way that a state is related to a city.  That info remains solely
in relationship setup.  The only info encoded in the method is the fact that
the state has a country, with nothing about exactly how the two are linked.

-John




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to