DBIx :: Class:Template Toolkit中的子结果集表示为数组,而不是结果集

时间:2015-02-04 15:35:30

标签: perl catalyst dbix-class template-toolkit

我正在使用DBIx :: Class和Template Toolkit开发Catalyst应用程序;在我遇到问题的特定部分中,我通过在ResultSet模式中调用以下函数来获得结果集:


    sub divisions_and_teams_in_season {
      my ( $self, $season, $grid ) = @_;

      return $self->search({
          "division_seasons.season"         => $season->id,
          "division_seasons.fixtures_grid"  => $grid->id,
        }, {
        prefetch  => [
          "division_seasons",
          {
            "team_seasons" => {
              "team" => [{
                  "club" => "venue"
                },
                "home_night"
              ]
            }
          }
        ],
        order_by  => {
          -asc => [ qw( division_seasons.rank team_seasons.grid_position club.short_name team.name ) ]
        }
      });
    }

这会返回我期望的数据,并且我可以在我的Controller代码中执行以下操作来获取结果集并遍历team_seasons:


    my $divisions = [ $c->model("DB::Division")->divisions_and_teams_in_season($current_season, $c->stash->{grid}) ];

    foreach my $division ( @{ $divisions } ) {
      $c->log->debug( $division->team_seasons->grid_positions_filled ); # This works because $division->team_seasons is a resultset object 
    }

但是,在我的模板中(隐藏$divisions),我无法访问grid_positions_filled对象,因为division.team_seaons在该部门中给出了一个团队结果集的arrayref:


    [%
    # Loop through our divisions
    FOREACH division IN divisions;
      CALL c.log.debug(division.team_seasons); # The output of this is something like: ARRAY(0x6f8318c)
    END;
    -%]

我在控制器中获得的相同调试日志的输出更像是结果集对象列表:


    TopTable::Model::DB::TeamSeason=HASH(0x6eea94c)
    TopTable::Model::DB::TeamSeason=HASH(0x6f01834)
    TopTable::Model::DB::TeamSeason=HASH(0x6ef5284)
    TopTable::Model::DB::TeamSeason=HASH(0x6efec9c)
    TopTable::Model::DB::TeamSeason=HASH(0x6ef4dc4)
    TopTable::Model::DB::TeamSeason=HASH(0x6faf0ac)
    TopTable::Model::DB::TeamSeason=HASH(0x6eefa04)

希望这一切都有道理!有谁知道如何从控制器获取行为到模板中,以便我可以访问team_season ResultSet上的方法?

非常感谢你。

2 个答案:

答案 0 :(得分:2)

尝试$self->search_rs而不是$self->search。 "此方法与search()完全相同,但它始终返回结果集,即使在列表上下文中也是如此。"

See the docs了解更多信息。

答案 1 :(得分:0)

team_seasons访问器在标量上下文中返回结果集,在列表上下文中返回行数组。似乎模板工具包代码在列表上下文中评估访问者。

作为解决方法,DBIC安装了一个特殊的访问器,后缀为_rs,无论上下文如何,它始终返回结果集。所以以下内容应该有效:

CALL c.log.debug(division.team_seasons_rs.grid_positions_filled);

这在DBIx::Class::Relationship下记录。