如何在DBIx :: Class中指定远程连接的列(删除了几个关系)?

时间:2013-07-05 05:53:13

标签: perl dbix-class

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

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

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

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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"