[html-formfu] Creating a new record

Ascii King tech at swattermatter.com
Wed Mar 18 18:31:21 GMT 2009


Apparently this is an RTFM issue. I see now that i should be using 
find_or_new to look for an existing record or call a new one. However, I 
do not know how to code the insert statement necessary to commit the 
record to the database. Can anyone show me this?

    my $book = $c->model('DB::Prior')->find_or_new({ prior_id => $id });

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

    if ($form->submitted_and_valid) {
      # the update should be an insert?
        $form->model->update($book);

        $c->stash->{status_msg} = 'Record ammended';
        $c->response->redirect($c->uri_for('/prior/list_prior'));
        $c->detach;
    } else {
        $form->model->default_values($book);
    }


Ascii King wrote:
> I have a has_many relationship between Character.pm and 
> CharacterBackground.pm  I apologize for the long names, but I was 
> trying to make sure it wasn't the naming that was causing the problem.
> When I call a new record from Character and use a repeatable element 
> to pull up and display the CharacterBackground, I get a repeated 
> option for every single record (all records) in the 
> CharacterBackground table. There should be no related records in the 
> CharacterBackground table when I call a new record, so I should only 
> get the one instance of a repeatable element that is caused by the 
> 'new_empty_row_multi' option.
>
> When I edit an existing record, the proper number of records show up 
> and everything works as expected.
>
> Thank you for your help.
>
>
> 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' );
>
>
>
>
> character.yml
> ---
> indicator: submitted
> elements:
>
> # NAME --
>    - type: Text
>      name: name
>      label: Name
>
> # THECOUNT --
>    - type: Hidden
>      name: characterbackground_counter
>
>    - type: Repeatable
>      nested_name: link_characterbackground_h
>      counter_name: characterbackground_counter
>      model_config:
>        new_empty_row_multi: background_name
>      elements:
>
>          - type: Hidden
>            name: char_back_id
>
> # BACKGROUND_NAME --
>          - type: Select
>            name: background_name
>            empty_first: 1
>
> # SUBMIT --
>    - type: Submit
>      name: submitted
>      value: Submit
>
>
> 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>
>
>
> 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';
> }
>
>
> Carl Franks wrote:
>> Hi Ascii,
>> Can you post your form config/code
>> and a basic outline of the relevant DBIC tables/relationships.
>>
>> Plus, explain what you want the Repeatable element to do.
>> Is the problem that it's not just editing the /related/ rows in the
>> 'many' table, but /all/ rows?
>>
>> Cheers,
>> Carl
>>   
>
> _______________________________________________
> HTML-FormFu mailing list
> HTML-FormFu at lists.scsys.co.uk
> http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu
>



More information about the HTML-FormFu mailing list