Belmark Caday
Belmark Caday

Reputation: 1683

How do to specify a column of a distant join (several relations removed) in DBIx::Class?

How do I specify in DBIx::Class the column of the third table I'm joining on? Here is my code:

my $join = $schema->resultset('Track')->search({'name' => 'Eminem'},{'join' => {'cd' => 'artist'}});

It just displays the tracks of Eminem, but I also want to display the artist name, Eminem? I cannot access name in that query because this is a ResultSet for Track, name is a column in the Artist table, the third table in the join.

Upvotes: 0

Views: 136

Answers (2)

Alexander Hartmaier
Alexander Hartmaier

Reputation: 2204

I guess you want to filter your resultset based on the artist name, not track name:

my $rs = $schema->resultset('Track')->search({
    'artist.name' => 'Eminem',
},
{
    join => { cd => 'artist' },
});

When looping through the resultset you can access it using the relationship and column accessors:

for my $track ($rs->all) {
    say $track->cd->artist->name . ' - ' . $track->name;
}

Upvotes: 1

daxim
daxim

Reputation: 39158

Use the relationship accessors.

for my $track (
    $schema->resultset('Track')->search(
        {
            name => 'Eminem',
        },
        {
            join => {cd => 'artist'},
        }
    )->all
) {
    use Data::Printer;
    p { $track->get_inflated_columns };
    p $track->cd->artist->name;
}
__END__
{
    cd        MyDatabase::Main::Result::Cd  {
        internals: {
            _column_data     {
                artist   2,
                cdid     3,
                title    "The Marshall Mathers LP"
            },
        }
    },
    title     "The Way I Am",
    trackid   1
}
"Eminem"
{
    cd        MyDatabase::Main::Result::Cd  {
        internals: {
            _column_data     {
                artist   2,
                cdid     3,
                title    "The Marshall Mathers LP"
            },
        }
    },
    title     "Stan",
    trackid   2
}
"Eminem"

Upvotes: 0

Related Questions