html-formhandler-model-dbic icon indicating copy to clipboard operation
html-formhandler-model-dbic copied to clipboard

HTML::FormHandler::TraitFor::Model::DBIC bungles objects with multiple primary keys

Open karenetheridge opened this issue 8 years ago • 0 comments

in HTML::FormHandler::TraitFor::Model::DBIC::set_item, the primary key(s) are used to calculate the $item_id, and then the $item_id is tested for truthiness. This check is incorrect for schema objects with multiple primary keys, as the item_id will always be true ([ { key1 => ..., key2 => ..., } ]). Calling $self->item_id($item_id) when the primary keys are undefined then results in an uninitialized warning in set_item_id.

I think this is the right change to make (I'm not sure how to test it):

--- a/lib/HTML/FormHandler/TraitFor/Model/DBIC.pm
+++ b/lib/HTML/FormHandler/TraitFor/Model/DBIC.pm
@@ -563,7 +563,10 @@ sub set_item {
     }
     elsif ( @primary_columns > 1 ) {
         my @pks = map { $_ => $item->get_column($_) } @primary_columns;
-        $item_id = [ { @pks }, { key => 'primary' } ];
+        # only set item_id if all PK columns are set
+        if (not grep { !$_ } @pks) {
+            $item_id = [ { @pks }, { key => 'primary' } ];
+        }
     }
     if ($item_id) {
         $self->item_id($item_id);

karenetheridge avatar Nov 29 '16 21:11 karenetheridge