Search code examples
perlcatalystdbix-classtemplate-toolkit

DBIx::Class: sub resultset in Template Toolkit presented as an array, not a resultset


I am developing a Catalyst application using DBIx::Class and Template Toolkit; in the particular part I'm having issues with, I have a resultset obtained using by calling the following function in my ResultSet schema:


    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 ) ]
        }
      });
    }

This returns the data as I would expect and I'm able to do the following in my Controller code to get back my resultset and iterate through the 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 
    }

However, in my template (having stashed $divisions), I'm unable to access the grid_positions_filled object because division.team_seaons gives me an arrayref of team resultsets in that division:


    [%
    # 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;
    -%]

The output I get for the same debug log in my controller is more like a list of resultset objects:


    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)

Hope all this makes sense! Does anyone know how I can get the behaviour from the controller into the template so that I can access methods on the team_season ResultSet?

Thank you very much in advance.


Solution

  • The team_seasons accessor returns a resultset in scalar context, and an array of rows in list context. It seems that template toolkit code evaluates the accessor in list context.

    As a work-around, DBIC installs a special accessor, postfixed with _rs that always returns a resultset regardless of context. So the following should work:

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

    This is documented under DBIx::Class::Relationship.