0

如何在DBIx::Class中指定要加入的第三个表的列?这是我的代码:

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

它只是显示 Eminem 的曲目,但我还想显示艺术家名称,Eminem?我无法访问name该查询,因为这是 Track 的 ResultSet,name是 Artist 表中的一列,是连接中的第三个表。

4

2 回答 2

1

我猜您想根据艺术家姓名过滤结果集,而不是曲目名称:

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

遍历结果集时,您可以使用关系和列访问器访问它:

for my $track ($rs->all) {
    say $track->cd->artist->name . ' - ' . $track->name;
}
于 2013-07-08T10:56:19.393 回答
0

使用关系访问器。

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"
于 2013-07-05T12:06:47.623 回答