[英]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; 我正在使用DBIx :: Class和Template Toolkit开发Catalyst应用程序; in the particular part I'm having issues with, I have a resultset obtained using by calling the following function in my ResultSet schema:
在遇到问题的特定部分,我通过在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 ) ] } }); }
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: 这将返回我期望的数据,并且我能够在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 }
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: 但是,在我的模板中(藏有
$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; -%]
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?
有谁知道我如何将行为从控制器添加到模板中,以便我可以访问team_season ResultSet上的方法?
Thank you very much in advance. 提前非常感谢您。
Try $self->search_rs
rather than $self->search
. 尝试
$self->search_rs
而不是$self->search
。 "This method does the same exact thing as search() except it will always return a resultset, even in list context." “此方法执行与search()相同的操作,除了即使在列表上下文中也总是返回结果集。”
See the docs for more info. 有关更多信息, 请参阅文档 。
The team_seasons
accessor returns a resultset in scalar context, and an array of rows in list context. team_seasons
访问器在标量上下文中返回结果集,在列表上下文中返回行数组。 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. 解决方法是,DBIC安装一个特殊的访问器,
_rs
后缀,无论上下文如何,该访问器始终返回结果集。 So the following should work: 因此,以下应该工作:
CALL c.log.debug(division.team_seasons_rs.grid_positions_filled);
This is documented under DBIx::Class::Relationship
. 这在
DBIx::Class::Relationship
下DBIx::Class::Relationship
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.