On Thu, Apr 19, 2012 at 6:42 PM, Robyn Jonahs <[email protected]>wrote:
> Hi,
>
> My question entails HTML::FormFu::Model::DBIC
>
> I am trying to set up a FormFu form where I have a table of locations and
> I want to associate a state with each one.
> So a location has_one state
> and a state has_many locations
>
> I want to have a controller for locations such that I can add a new
> location and pick the state from a select element where I get the values
> from the State Schema/Result
>
>
> So I made databases with tables "locations" and "states"
>
> locations has columns
> id state_id name ...
>
> states has columns
> id state ...
>
>
> I avoided setting foreign keys and index columns in the sql and set them
> at the bottom of the Schema::Result::State.pm and Location.pm modules.
>
>
>
> State.pm
> ...
> =head1 RELATIONS
>
> =head2 locations
>
> Type: has_many
>
> Related object: L<LT::Schema::Result::Location>
>
> =cut
>
> __PACKAGE__->has_many(
> "locations",
> "LT::Schema::Result::Location",
> { "foreign.state_id" => "self.id" },
> { cascade_copy => 0, cascade_delete => 0 },
> );
> 1;
>
>
>
> Location.pm
> ...
> =head2 state
>
> Type: belongs_to
>
> Related object: L<LT::Schema::Result::State>
>
> =cut
>
> __PACKAGE__->belongs_to(
> "state",
> "LT::Schema::Result::State",
> { id => "state_id" },
> { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
> );
> 1;
>
>
> In my controller Locations.pm, I get all the state values to set the
> options on the select element
> ...
> # States
> my @state_objs = $c->model("DB::State")->all();
> my @state;
> foreach (sort {$a->state cmp $b->state} @state_objs) {
> push(@state, [$_->id, $_->state]);
> }
> my $select5 = $form->get_element({name => 'state'});
> $select5->options(\@state);
> ...
>
> This works and sets the options on the form when rendered....
>
>
>
> YAML form definition has
>
> ---
> # indicator is the field that is used to test for form submission
> indicator: submit
> # Start listing the form elements
> elements:
>
> ...
> # State or Province
> - type: Select
> size: 1
> name: state
> label: State | Province
> constraints:
> - Required
> ...
>
> # Submit button
> - type: Submit
> name: submit
> value: Submit
>
> # Global filters and constraints.
> filter:
> # Remove whitespace at both ends
> - TrimEdges
> # Escape HTML characters for safety
> - HTMLEscape
>
> However, this does not update the columns in location for the state_id
> field.
>
> I can not figure out why it is not adding these values or what is going
> wrong with my use of FormFu.
>
> This is in reverse of the example in
> perldoc HTML::FormFu::Model::DBIC
> where there is one book that has many reviews and the form that is
> generated is for the book controller it seems.
>
> I really do not want to list a state and then all the locations that is
> associated with, I want to add | update a location and be able to select a
> state from a select form element and have it stick that id into the
> state_id in the locations database table. Am I missing something
> fundamental or should I be working on some more code in the Locations
> controller to accomplish this task?
>
> Thanks again for the advice.
> R
>
>
Solved. By trial and error and some debug code, I just learned that FormFu
has to map 1:1 to the model field names to update them. Not the associated
table accessors in the relationship declarations. I am climbing the steep
learning curve.
so my YAML name should be state_id and also that has to be fixed in the
controller.
:( R
_______________________________________________
HTML-FormFu mailing list
[email protected]
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu