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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Chris Welch
Hi folks

I'm relatively new to this, so sorry if this is a silly question - and sorry if it goes to the wrong place (I'm still not sure it's not a Template Toolkit issue):

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 I guess 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.


Chris



_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

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

Chris Welch

On 4 February 2015 at 17:23, Peter Rabbitson <[hidden email]> wrote:
On 02/04/2015 06:14 PM, Chris Welch wrote:

 ...

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

...


Any relationship accessor is also created with an <accessor_name>_rs variant, to work around *exactly* this (mis)feature of TT.

What you want above is ...division.team_seasons_rs...

Cheers


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...

​Fantastic, thank you so much for that!  I'm glad it was that easy...!​


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

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

Trevor Leffler
In reply to this post by Chris Welch
On 02/04/2015 09:23 AM, Peter Rabbitson wrote:

> On 02/04/2015 06:14 PM, Chris Welch wrote:
>>
>>  ...
>>
>> 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;
>>      -%]
>>
>> ...
>>
>
> Any relationship accessor is also created with an <accessor_name>_rs
> variant, to work around *exactly* this (mis)feature of TT.
>
> What you want above is ...division.team_seasons_rs...
>
> Cheers

And to expound on *why* this is happening... TT works in list context by
default, so anything that can return a list of values will do so.
Meaning those resultset calls (which usually come down to search()'s)
return lists of rows versus another resultset object.

There are a couple of ways to tell TT to work in scalar context, one of
which Peter described above.  Another is to use TT's Scalar plugin [1]
and do something like ...division.scalar.team_seasons...

I'd be interested in about hearing folks' experiences/pros/cons of using
_rs vs scalar vs ??? in TT.

--Trevor

[1] http://www.tt2.org/docs/modules/Template/Plugin/Scalar.html

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...