[Catalyst] Too many records returned

Ascii King tech at swattermatter.com
Wed Mar 18 13:34:18 GMT 2009


I have a sub that is supposed to call a record for editing if an $id 
parameter is specified. If no $id is given, then it is supposed to 
create a new record. It works fine when used to call an existing 
record.  The problem I am having is that it is returning every record in 
the table associated by  a has_many relationship when no $id file is 
given (new record).

 Character ->has_many->CharacterBackground

What I should get is zero records returned from the CharacterBackground 
table because it is a new Character record. What I get is every single 
record in the CharacterBackground table returned. What is wrong with my 
code?

Character.pm
-----------------------------------
sub character : Local FormConfig('character/character.yml') {
   my ($self, $c, $id) = @_;

   my $book;

   if (defined($id)) {

       $book = $c->model('DB::Character')->find($id);

       unless ($book) {
           $c->stash->{error_msg} = "Invalid Character record -- Cannot 
edit";
           $c->response->redirect($c->uri_for('list_character'));
           $c->detach;
       }
   } else {

       $book = $c->model('DB::Character')->new_result({});
   }

   my $form = $c->stash->{form};

   if ($form->submitted_and_valid) {
       $form->model->update($book);
       $c->stash->{status_msg} = 'Record ammended';
       $c->response->redirect($c->uri_for('list_character'));
       $c->detach;
   } else {
       $form->model->default_values($book);
   }

   $c->stash->{given_title} = 'Edit Character';
   $c->stash->{template} = 'character/character.tt2';
}


Tables:
- - - - - - - - - - - - - -
Character.pm
fields:
   character_id
   name
   etc..
   set_primary_key("character_id")

__PACKAGE__->has_many('link_characterbackground_h' => 
'Orpheus::Schema::CharacterBackground', 'character_id');

CharacterBackground.pm
fields:
   char_back_id
   character_id
   etc...
   set_primary_key("char_back_id")
__PACKAGE__->belongs_to('link_charbackchar_b' => 
'Orpheus::Schema::Character', 'character_id' );



Links:
- - - - - - - - - - - - - - - - -
Here is the code that I use to edit a record. It works properly.
<a href="[% c.uri_for('character', char_rec.character_id) %]">Edit</a>

Here is the code I use to call for a new record.
<a href="[% c.uri_for('character') %]">Create a New Character</a>






More information about the Catalyst mailing list