0

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.

daxim
  • 39,270
  • 4
  • 65
  • 132
Belmark Caday
  • 1,623
  • 3
  • 21
  • 29

2 Answers2

1

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;
}
Alexander Hartmaier
  • 2,178
  • 12
  • 21
0

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"
daxim
  • 39,270
  • 4
  • 65
  • 132