Reputation: 1683
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
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
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