I've recently started using HTML::FormFu::Model::DBIC, and having
great success, except for a situation where I was trying to have a
form submission create a new record, and create a related record at
the same time. Essentially, the main/primary record was being created,
but the related record was not (or not being associated with the
primary record, at least.)

I hope I'm not overlooking anything obvious, but I've attached a patch
with a test demonstrating the basic case, and a fix the problem. All
of the other tests continue to pass, under both DBIx::Class 0.080101
and v0.08099_05 (r5047).

I've also included the patch below.

Thanks,

-A

Index: t/create/belongs_to.yml
===================================================================
--- t/create/belongs_to.yml     (revision 0)
+++ t/create/belongs_to.yml     (revision 0)
@@ -0,0 +1,20 @@
+---
+auto_fieldset: 1
+model_config:
+  resultset: Master
+
+elements:
+  - type: Hidden
+    name: id
+
+  - type: Text
+    name: text_col
+
+  - type: Block
+    nested_name: type
+    elements:
+      - type: Text
+        name: type
+
+  - type: Submit
+    name: submit
Index: t/create/belongs_to.t
===================================================================
--- t/create/belongs_to.t       (revision 0)
+++ t/create/belongs_to.t       (revision 0)
@@ -0,0 +1,42 @@
+use strict;
+use warnings;
+use Test::More tests => 3;
+
+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/create/belongs_to.yml');
+
+my $schema = MySchema->connect('dbi:SQLite:dbname=t/test.db');
+
+# put schema on form stash
+$form->stash->{schema} = $schema;
+
+# Fake submitted form
+$form->process(
+    {   id          => 1,
+        text_col    => 'foo',
+        'type.type' => 'bar',
+    }
+);
+
+{
+
+    # no resultset arg
+    $form->model->create;
+}
+
+{
+    my $row = $schema->resultset('Master')->find(1);
+
+    is( $row->text_col,   'foo' );
+    is( $row->type_id,    3 );
+    is( $row->type->type, 'bar' );
+}
+
Index: lib/HTML/FormFu/Model/DBIC.pm
===================================================================
--- lib/HTML/FormFu/Model/DBIC.pm       (revision 1226)
+++ lib/HTML/FormFu/Model/DBIC.pm       (working copy)
@@ -369,6 +369,7 @@
     _save_columns( $base, $dbic, $form ) or return;

     $dbic->update_or_insert;
+    $dbic->discard_changes;

     _save_relationships( $self, $base, $dbic, $form, $rs, $attrs, [EMAIL 
PROTECTED] );

@@ -449,6 +450,9 @@
                     nested_base => $rel,
                     from        => $dbic->result_class,
                 } );
+
+            $dbic->set_from_related( $rel, $target );
+            $dbic->update();
         }
     }
 }



-- 
Adam Herzog
Email:  [EMAIL PROTECTED]

Attachment: create_belongs_to.diff
Description: Binary data

_______________________________________________
HTML-FormFu mailing list
HTML-FormFu@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu

Reply via email to