[Catalyst] In HTML::FormHandler search form (no DB schema) -- how to populate a SELECT field via DBIx::Class?

will trillich will.trillich at serensoft.com
Sun Jan 16 15:41:41 GMT 2011


*Short version*: If you have a HTML::FormHandler search form without a
"has_field '+item_class'" (there's no data-base row corresponding to its
contents) how to you reach your DBIx::Class schemas to populate a drop-down
menu?


*Long version*: This is in the context of a Catalyst app -- however, I do
know this is either FormHandler question or a DBIC question, and not
intrinsically a Catalyst question, but... hoping the wise folks here can
nudge me in the right direction...

We have several DBIC resultsets including Incident and Country. The Incident
record has this relationship:
__PACKAGE__->belongs_to( 'country' =3D>
'Incident::Schema::DB::Result::Country' );

(And the country schema has_many incidents, of course)

For the CRUD form related to the actual incident table, we have a matching
HTML::FormHandler package with its
has '+item_class'
as expected. This makes it easy to create a
sub options_country {
  my $self =3D shift;
  return unless $self->schema;
  my $rs =3D $self->schema->resultset('Country');
  #...
}
subroutine for the form, for populating a country drop-down menu in the
HTML.

But!

For the SEARCH form, we are not interested in CRUD, plus we have extra
fields (for example, we want the user to be able to bracket dates with a
start-field and an end-field) so there's no one-to-one correlation with the
database table -- meaning there's no "item_class" for the search form. And
hence no schema to rely on.

What we're looking for now -- unless there's a better more obvious paradigm
-- is a way to get from an instance of the schema-object, back to the
resultset, for searching.
sub options_country {
  my $self =3D shift; # note $c is not available here :(
  # $self->schema will be UNDEF here
  my $obj =3D Spill::Schema::DB::Result::Country->new; # Hard-wired, yes, is
there a better way?
  my $rs =3D $obj->???
  # ?
}

I've looked at
- $obj->result_source (Can't call method "resolve" on an undefined valued in
DBIx/Class/Row)
- $obj->result_source_instance->resultset (Can't call method
"default_resultset_attributes" on an undef value in DBIx/Class/ResultSource)
- $obj->result_source_instance->resultset_class->search({},{}) (Can't use
string as a HASH ref in DBIx/Class/ResultSet)
and a couple other dead ends.

It's likely to be something obvious I've overlooked. :( Suggestions?

-- =

The first step towards getting somewhere is to decide that you are not going
to stay where you are.  -- J.P.Morgan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20110116/e42c6=
22f/attachment.htm


More information about the Catalyst mailing list