http://www.perlmonks.org?node_id=709184

uG has asked for the wisdom of the Perl Monks concerning the following question:

Basically what I have included is the dbix::class schema's for my catalyst app as well as the view function for it. It seems simple enough, and I figured:
[% FOR resource = nation.resources %]<li>[% resource %]</li>[% END %]
Would print out each resource name for each resource id a nation had. I was quite surprised when it didn't work, but did when I changed it to:
[% FOR resource = nation.resources %]<li>[% resource.resource %]</li>[ +% END %]
Now I basically cloned this whole thing from the catalyst roles tutorial. The only difference between that and this is that is in this one I have to put the nation object into the stash, whereas with the tutorial you can access it through c (thanks to authorization via c.user.roles). What am I missing here?
--------------------------------------------------------------
sub view_nation : Regex('^nation/(\d+)$') { my ( $self, $c ) = @_; my $nid = $c->request->snippets->[0]; $c->stash->{nation} = $c->model('DB::Nation')->find({id => $nid}) | +| die "No such nation!"; $c->stash->{template} = 'nation/nation.tt2'; }
package Game::Schema::Nation; use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/PK::Auto Core/); __PACKAGE__->table('nation_info'); __PACKAGE__->add_columns(qw/id name owner/); __PACKAGE__->set_primary_key('id'); __PACKAGE__->belongs_to(user => 'Game::Schema::User', 'owner'); __PACKAGE__->has_many(map_nation_resource => 'Game::Schema::NationReso +urce', 'nation_id'); __PACKAGE__->many_to_many(resources => 'map_nation_resource', 'resourc +e'); =head1 NAME Game::Schema::Nation - A model object representing a nation. =cut 1;
package Game::Schema::NationResource; use base qw/DBIx::Class/; # Load required DBIC stuff __PACKAGE__->load_components(qw/PK::Auto Core/); # Set the table name __PACKAGE__->table('nation_resources'); # Set columns in table __PACKAGE__->add_columns(qw/nation_id resource_id/); # Set the primary key for the table __PACKAGE__->set_primary_key(qw/nation_id resource_id/); __PACKAGE__->belongs_to(nation => 'Game::Schema::Nation', 'nation_id') +; __PACKAGE__->belongs_to(resource => 'Game::Schema::Resource', 'resourc +e_id'); =head1 NAME Game::Schema::NationResource - A model object representing the JOIN be +tween Nations and Resources. =cut 1;
package Game::Schema::Resource; use base qw/DBIx::Class/; # Load required DBIC stuff __PACKAGE__->load_components(qw/PK::Auto Core/); # Set the table name __PACKAGE__->table('resources'); # Set columns in table __PACKAGE__->add_columns(qw/id resource/); # Set the primary key for the table __PACKAGE__->set_primary_key('id'); __PACKAGE__->has_many(map_nation_resource => 'Game::Schema::NationReso +urce', 'resource_id'); =head1 NAME Game::Schema::Resources - A model object representing a resource to a +nation. =cut 1;

Replies are listed 'Best First'.
Re: dbix::class relationship problem or TT object handling problem?
by edoc (Chaplain) on Sep 05, 2008 at 10:01 UTC

    looks ok to me, nation.resources returns a list of resource objects so you need to access the column in the object..

    you could also do:

    [% FOR resource = nation.resources %]<li>[% resource.id %]</li>[% END +%]

    which would list the resouce ids..

    cheers,

    J

Re: dbix::class relationship problem or TT object handling problem?
by jasonk (Parson) on Sep 05, 2008 at 18:08 UTC