[html-formfu] belongs_to and has_many Example Locations and states

Robyn Jonahs learn.catalyst at gmail.com
Thu Apr 19 22:42:58 GMT 2012


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
...
=3Dhead1 RELATIONS

=3Dhead2 locations

Type: has_many

Related object: L<LT::Schema::Result::Location>

=3Dcut

__PACKAGE__->has_many(
  "locations",
  "LT::Schema::Result::Location",
  { "foreign.state_id" =3D> "self.id" },
  { cascade_copy =3D> 0, cascade_delete =3D> 0 },
);
1;



Location.pm
...
=3Dhead2 state

Type: belongs_to

Related object: L<LT::Schema::Result::State>

=3Dcut

__PACKAGE__->belongs_to(
  "state",
  "LT::Schema::Result::State",
  { id =3D> "state_id" },
  { is_deferrable =3D> 1, on_delete =3D> "CASCADE", on_update =3D> "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 =3D $c->model("DB::State")->all();
        my @state;
        foreach (sort {$a->state cmp $b->state} @state_objs) {
          push(@state, [$_->id, $_->state]);
        }
        my $select5 =3D $form->get_element({name =3D> '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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/html-formfu/attachments/20120419/a1=
fa8892/attachment.htm


More information about the HTML-FormFu mailing list