[html-formfu] Model-DBIC - Creating related records

Adam Herzog adam at adamherzog.com
Mon Nov 3 18:27:05 GMT 2008


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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- 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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- 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 =3D> 3;
+
+use HTML::FormFu;
+use lib 't/lib';
+use DBICTestLib 'new_db';
+use MySchema;
+
+new_db();
+
+my $form =3D HTML::FormFu->new;
+
+$form->load_config_file('t/create/belongs_to.yml');
+
+my $schema =3D MySchema->connect('dbi:SQLite:dbname=3Dt/test.db');
+
+# put schema on form stash
+$form->stash->{schema} =3D $schema;
+
+# Fake submitted form
+$form->process(
+    {   id          =3D> 1,
+        text_col    =3D> 'foo',
+        'type.type' =3D> 'bar',
+    }
+);
+
+{
+
+    # no resultset arg
+    $form->model->create;
+}
+
+{
+    my $row =3D $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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- 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, \@rels );

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



-- =

Adam Herzog
Email:  adam at adamherzog.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: create_belongs_to.diff
Type: application/octet-stream
Size: 2084 bytes
Desc: not available
Url : http://lists.scsys.co.uk/pipermail/html-formfu/attachments/20081103/b=
6c32375/create_belongs_to.obj


More information about the HTML-FormFu mailing list