--- lib/HTML/FormFu/Model/DBIC.pm 2009-05-28 18:24:29.000000000 +0200 +++ lib/HTML/FormFu/Model/DBIC.pm.new 2009-05-28 18:34:40.000000000 +0200 @@ -883,12 +883,13 @@ my @values = $form->param_list($nested_name); my @rows; - if (@values) { my $config = _compatible_config($field); my ($pk) = $config->{default_column} || $related->result_source->primary_columns; + if (@values) { + $pk = "me.$pk" unless $pk =~ /\./; @rows = $related->result_source->resultset->search( { @@ -896,9 +897,25 @@ $pk => { -in => \@values } } )->all; } + if($config->{additive}) { + + my $relinfo = $dbic->result_source->relationship_info($name); + use Data::Dumper; warn Dumper $relinfo; + + $pk =~ s/^.*\.//; + + my $set_method = "add_to_$name"; + my $remove_method = "remove_from_$name"; + + foreach my $row ( @rows ) { + $dbic->$remove_method($row); + $dbic->$set_method($row, $config->{link_values}); + } + } else { my $set_method = "set_$name"; - $dbic->$set_method( \@rows ); + $dbic->$set_method( \@rows, $config->{link_values} ); + } } } } @@ -1396,6 +1413,34 @@ model_config: default_column: foo +If you want to set columns on the link table you can do so if you add a +C attribute to C: + + --- + element: + - type: Checkboxgroup + name: authors + model_config: + link_values: + foo: bar + + +The default implementation will first remove all related objects and set the +new ones (see L). +If you want to add the selected objects to the current set of objects +set C in the C. + + --- + element: + - type: Checkboxgroup + name: authors + model_config: + additive: 1 + options_from_model: 0 + +( is set to C<0> because this L will try to fetch +all objects from the result class C if C is specified +without a C attribute.) =head1 COMMON ARGUMENTS --- t/update/many_to_many_select.t 2009-05-26 21:10:21.000000000 +0200 +++ t/update/many_to_many_select.t.bak 2009-05-28 18:37:01.000000000 +0200 @@ -1,6 +1,6 @@ use strict; use warnings; -use Test::More tests => 5; +use Test::More tests => 14; use HTML::FormFu; use lib 't/lib'; @@ -39,6 +39,9 @@ # band 3 - not used $schema->resultset('Band')->create({ band => 'the kinks' }); + # band 4 - not used + $schema->resultset('Band')->create({ band => 'the hoorays' }); + # band 1 $u2->add_to_bands($band1); } @@ -75,3 +78,73 @@ is( $id[1], 3 ); } +# additive and link_value + +$form = HTML::FormFu->new; + +$form->load_config_file('t/update/many_to_many_select_additive.yml'); + +$form->stash->{schema} = $schema; + +{ + $form->process( { + id => 2, + name => 'Paul McCartney', + bands => 2, + } ); + + ok( $form->submitted_and_valid ); + + my $row = $schema->resultset('User')->find(2); + + $form->model->update($row); +} + +{ + my $row = $schema->resultset('User')->find(2); + + is( $row->name, 'Paul McCartney' ); + + my @bands = $row->bands->all; + + is( scalar @bands, 3 ); + + my @id = sort map { $_->id } @bands; + + is_deeply( \@id, [qw(1 2 3)] ); +} + +{ + $form->get_field('bands')->model_config->{link_values} = {rating => 200}; + + $form->process( { + id => 2, + name => 'Paul McCartney', + bands => [qw(1 2 3 4)], + } ); + + ok( $form->submitted_and_valid ); + + my $row = $schema->resultset('User')->find(2); + + $form->model->update($row); +} + +{ + my $row = $schema->resultset('User')->find(2); + + is( $row->name, 'Paul McCartney' ); + + my @bands = $row->bands->all; + + is( scalar @bands, 4 ); + + my @id = sort map { $_->id } @bands; + + is_deeply( \@id, [qw(1 2 3 4)] ); + + my @link = map { $_->rating } $row->user_bands->all; + + is_deeply( \@link, [qw(200 200 200 200)] ); + +} \ No newline at end of file