Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions lib/HTML/FormFu/Model/DBIC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,32 @@ sub update {
my @rels = $rs->relationships;
my @cols = $rs->columns;

# check for belongs_to relationships with a required foreign key
my (@mandatory_rels, @non_mandatory_rels);

foreach my $rel (@rels) {
# 'fk_columns' is set for belong_to rels in DBIx::Class::Relationship::BelongsTo
my @fk_columns = keys %{ $dbic->relationship_info($rel)->{attrs}{fk_columns} };

if ( @fk_columns and notall { $dbic->column_info($_)->{is_nullable} } @fk_columns ) {
push @mandatory_rels, $rel;
} else {
push @non_mandatory_rels, $rel;
}
}

# add required belongs_to rels before insert
if (@mandatory_rels) {
# tell _save_relationships not to update $dbic yet, just add the rels
my %attrs = ( %$attrs, no_update => 1 );
_save_relationships( $self, $base, $dbic, $form, $rs, \%attrs, \@mandatory_rels );
}

_save_columns( $base, $dbic, $form ) or return;

$dbic->update_or_insert;

_save_relationships( $self, $base, $dbic, $form, $rs, $attrs, \@rels );
_save_relationships( $self, $base, $dbic, $form, $rs, $attrs, \@non_mandatory_rels );

_save_multi_value_fields_many_to_many( $base, $dbic, $form, $attrs, \@rels,
\@cols );
Expand Down Expand Up @@ -462,7 +483,7 @@ sub _save_relationships {
} );
unless ( $dbic->$rel ) {
$dbic->$rel($target);
$dbic->update;
$dbic->update unless $attrs->{no_update};
}
}
elsif ( defined $multi_value ) {
Expand Down
26 changes: 26 additions & 0 deletions t/lib/MySchema/ManagedBand.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package MySchema::ManagedBand;
use strict;
use warnings;

use base 'DBIx::Class';

__PACKAGE__->load_components(qw/ Core /);

__PACKAGE__->table("managed_band");

__PACKAGE__->add_columns(
id => { data_type => "INTEGER", is_nullable => 0 },
manager_id => { data_type => "INTEGER", is_nullable => 0 },
band => { data_type => "TEXT", is_nullable => 0 },
);

__PACKAGE__->set_primary_key("id");

__PACKAGE__->belongs_to( manager => 'MySchema::Manager', 'manager_id' );

__PACKAGE__->has_many( user_bands => 'MySchema::UserBand', 'band' );

__PACKAGE__->many_to_many( users => 'user_bands', 'user' );

1;

37 changes: 37 additions & 0 deletions t/update/belongs_to_create_required.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use strict;
use warnings;
use Test::More tests => 2;

use HTML::FormFu;
use lib 't/lib';
use DBICTestLib 'new_schema';
use MySchema;

my $form = HTML::FormFu->new;

$form->populate({
elements => [
{
name => 'band',
},
{
type => 'Block',
nested_name => 'manager',
elements => {
name => 'name',
}
}
]
});

my $schema = new_schema();

my $rs = $schema->resultset('ManagedBand');
my $band = $rs->new_result({});

$form->process({ band => 'The Foobars', 'manager.name' => 'Mr Foo' });

$form->model('DBIC')->update($band);

is($band->band, 'The Foobars');
is($band->manager->name, 'Mr Foo');