[Catalyst-commits] r8283 - in CatalystX-CRUD/CatalystX-CRUD-Controller-RHTMLO/trunk: . lib/CatalystX/CRUD/Controller

karpet at dev.catalyst.perl.org karpet at dev.catalyst.perl.org
Mon Aug 25 18:33:18 BST 2008


Author: karpet
Date: 2008-08-25 18:33:18 +0100 (Mon, 25 Aug 2008)
New Revision: 8283

Modified:
   CatalystX-CRUD/CatalystX-CRUD-Controller-RHTMLO/trunk/Changes
   CatalystX-CRUD/CatalystX-CRUD-Controller-RHTMLO/trunk/lib/CatalystX/CRUD/Controller/RHTMLO.pm
Log:
fix auto-pk bug that dbic brings to light; release 0.16

Modified: CatalystX-CRUD/CatalystX-CRUD-Controller-RHTMLO/trunk/Changes
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD-Controller-RHTMLO/trunk/Changes	2008-08-25 17:28:56 UTC (rev 8282)
+++ CatalystX-CRUD/CatalystX-CRUD-Controller-RHTMLO/trunk/Changes	2008-08-25 17:33:18 UTC (rev 8283)
@@ -58,4 +58,7 @@
 
 0.15    24 Aug 2008
         * fix the form_to_object() method to work with 0.29 core REST api
+
+0.16    25 Aug 2008
+        * refactor the form_to_object() to avoid setting the PK value for a new object
  

Modified: CatalystX-CRUD/CatalystX-CRUD-Controller-RHTMLO/trunk/lib/CatalystX/CRUD/Controller/RHTMLO.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD-Controller-RHTMLO/trunk/lib/CatalystX/CRUD/Controller/RHTMLO.pm	2008-08-25 17:28:56 UTC (rev 8282)
+++ CatalystX-CRUD/CatalystX-CRUD-Controller-RHTMLO/trunk/lib/CatalystX/CRUD/Controller/RHTMLO.pm	2008-08-25 17:33:18 UTC (rev 8283)
@@ -4,7 +4,7 @@
 use Carp;
 use Class::C3;
 
-our $VERSION = '0.15';
+our $VERSION = '0.16';
 
 =head1 NAME
 
@@ -128,12 +128,31 @@
     # 1-to-1 mapping of form fields to object methods.
     $form->$form_meth($obj);
 
-    # set param values from request
-    $form->params( $c->req->params );
-    for my $field ( keys %pk ) {
-        $form->param( $field => $pk{$field} );
+    # set param values from request.
+    # we dereference req->params in order to avoid setting $id
+    # if it is an emptry string (as when submitting from create).
+    # This is mostly to fix the case where the PK is an auto-increment
+    # field, which we do not want to set in the object.
+    my %params = %{ $c->req->params };
+    if ( !$id ) {
+        my $pk_field = $self->primary_key;
+        if (    !ref($pk_field)
+            and defined $params{$pk_field}
+            and !length $params{$pk_field} )
+        {
+            delete $params{$pk_field};
+        }
     }
+    $form->params( \%params );
 
+    # set PKs specifically, in case they are not submitted
+    # explicitly in form
+    if ($id) {
+        for my $field ( keys %pk ) {
+            $form->param( $field => $pk{$field} );
+        }
+    }
+
     # override form's values with those from params
     # no_clear is important because we already initialized with object
     # and we do not want to undo those mods.
@@ -152,22 +171,9 @@
     # re-set object's values from the now-valid form
     # TODO this might not work if the delegate() does not have
     # 1-to-1 mapping of form fields to object methods.
-    # this is same objection as $form_metho call above
+    # this is same objection as $form_method call above
     $form->$obj_meth($obj);
 
-    # set PK(s) explicitly
-    for my $field ( keys %pk ) {
-        $obj->$field( $pk{$field} );
-    }
-
-    # let serial column work its magic
-    # if this is a first-time save (create)
-    #carp "serial column magic. id = $id. pk = " . Data::Dump::dump \%pk;
-    my $pk_method = $self->primary_key;
-    if ( !$id and !ref($pk_method) ) {
-        $obj->$pk_method(undef) if ( defined $obj->$pk_method );
-    }
-
     #Data::Dump::dump $obj;
 
     return $obj;




More information about the Catalyst-commits mailing list