Index: /Users/mo/Documents/workspace/HTML-FormFu-Model-DBIC/t/update/belongs_to_select.yml =================================================================== --- /Users/mo/Documents/workspace/HTML-FormFu-Model-DBIC/t/update/belongs_to_select.yml (revision 0) +++ /Users/mo/Documents/workspace/HTML-FormFu-Model-DBIC/t/update/belongs_to_select.yml (revision 0) @@ -0,0 +1,8 @@ +--- + model_config: + resultset: Note + elements: + - name: id + - name: note + - name: master + type: Select \ No newline at end of file Index: /Users/mo/Documents/workspace/HTML-FormFu-Model-DBIC/t/update/belongs_to_select.t =================================================================== --- /Users/mo/Documents/workspace/HTML-FormFu-Model-DBIC/t/update/belongs_to_select.t (revision 0) +++ /Users/mo/Documents/workspace/HTML-FormFu-Model-DBIC/t/update/belongs_to_select.t (revision 0) @@ -0,0 +1,35 @@ +use strict; +use warnings; +use Test::More tests => 2; + +use HTML::FormFu; +use lib 't/lib'; +use DBICTestLib 'new_db'; +use MySchema; + +new_db(); + +my $form = HTML::FormFu->new; + +$form->load_config_file('t/update/belongs_to_select.yml'); + +my $schema = MySchema->connect('dbi:SQLite:dbname=t/test.db'); + +$form->stash->{schema} = $schema; + +my $rs = $schema->resultset('Master'); + +$rs->create( { id => 1 } ); +$rs->create( { id => 2 } ); + +my $master = $rs->create( { text_col => 'b', type_id => 2, type2_id => 2 } ); + +$form->process( { master => 2, note => 'foo' } ); + +$form->model->create; + +my $note = $schema->resultset("Note")->find(1); + +is($note->master->id, 2); + +is($note->note, 'foo'); Index: /Users/mo/Documents/workspace/HTML-FormFu-Model-DBIC/t/update/many_to_many_select_additive.yml =================================================================== --- /Users/mo/Documents/workspace/HTML-FormFu-Model-DBIC/t/update/many_to_many_select_additive.yml (revision 0) +++ /Users/mo/Documents/workspace/HTML-FormFu-Model-DBIC/t/update/many_to_many_select_additive.yml (revision 0) @@ -0,0 +1,21 @@ +--- +auto_fieldset: 1 + +elements: + - type: Hidden + name: id + + - type: Text + name: name + + - type: Select + name: bands + multiple: 1 + model_config: + additive: 1 + options_from_model: 0 + link_values: + rating: 100 + + - type: Submit + name: submit Index: /Users/mo/Documents/workspace/HTML-FormFu-Model-DBIC/t/lib/MySchema/Note.pm =================================================================== --- /Users/mo/Documents/workspace/HTML-FormFu-Model-DBIC/t/lib/MySchema/Note.pm (revision 1508) +++ /Users/mo/Documents/workspace/HTML-FormFu-Model-DBIC/t/lib/MySchema/Note.pm (working copy) @@ -16,7 +16,7 @@ __PACKAGE__->set_primary_key("id"); -__PACKAGE__->belongs_to( master => 'MySchema::Master', 'id' ); +__PACKAGE__->belongs_to( master => 'MySchema::Master' ); 1; Index: /Users/mo/Documents/workspace/HTML-FormFu-Model-DBIC/lib/HTML/FormFu/Model/DBIC.pm =================================================================== --- /Users/mo/Documents/workspace/HTML-FormFu-Model-DBIC/lib/HTML/FormFu/Model/DBIC.pm (revision 1508) +++ /Users/mo/Documents/workspace/HTML-FormFu-Model-DBIC/lib/HTML/FormFu/Model/DBIC.pm (working copy) @@ -487,17 +487,46 @@ } ); } elsif ( defined $multi_value ) { - # has_one or might_have relationship + # belongs_to, has_one or might_have relationship my $info = $dbic->result_source->relationship_info($rel); - + my @fpkey = $dbic->related_resultset($rel)->result_source->primary_columns; - croak 'multiple primary keys are not supported for has_one/might_have relationships' - if(@fpkey > 1); + my @cond = (%{$info->{cond}}); + # make sure $rel is a has_one or might_have rel + # stolen from SQL/Translator/Parser/DBIx/Class + + my $fk_constraint; + + # Get the key information, mapping off the foreign/self markers + my @refkeys = map {/^\w+\.(\w+)$/} @cond; + my @keys = map {$info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond; + + #first it can be specified explicitly + if ( exists $info->{attrs}{is_foreign_key_constraint} ) { + $fk_constraint = $info->{attrs}{is_foreign_key_constraint}; + } + + # it can not be multi + elsif ( $info->{attrs}{accessor} + && $info->{attrs}{accessor} eq 'multi' ) { + $fk_constraint = 0; + } + + # if indeed single, check if all self.columns are our primary keys. + # this is supposed to indicate a has_one/might_have... + # where's the introspection!!?? :) + else { + $fk_constraint = not $dbic->result_source->compare_relationship_keys( \@keys, \@fpkey ); + } + + next if($fk_constraint); + + my $fpkey = shift @fpkey; - my ( $fkey, $skey ) = %{ $info->{cond} }; + my ( $fkey, $skey ) = @cond; $fkey =~ s/^foreign\.//; $skey =~ s/^self\.//; @@ -505,6 +534,12 @@ croak 'The primary key and the foreign key may not be the same column in class '.$fclass if $fpkey eq $fkey; + + croak 'multiple primary keys are not supported for has_one/might_have relationships' + if(@fpkey > 1); + + + my $schema = $dbic->result_source->schema;