Skip Menu |

This queue is for tickets about the Catalyst-Controller-DBIC-API CPAN distribution.

Report information
The Basics
Id: 65168
Status: resolved
Priority: 0/
Queue: Catalyst-Controller-DBIC-API

People
Owner: Nobody in particular
Requestors: AGORMAN [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 2.002004
Fixed in: 2.008001



Subject: Allow for the creation of multiple related objects
Doing a POST to a controller whose schema class has a many to many relationship doesn't allow for creating multiple related objects. For example if I pass the following data hash: data:{ ... my_has_many_relation:[{ ... }] ... } An error occurs because validate_object doesn't allow related params to be an array reference. The patch fixes that problem by allowing related params to be an array reference inside the validate_objects sub and then adding each related row in the insert_object_from_params sub.
Subject: API.pm.patch
--- /root/perl5/perlbrew/perls/perl-5.10.1/lib/site_perl/5.10.1/Catalyst/Controller/DBIC/API.pm 2010-03-31 16:41:32.000000000 -0700 +++ API.pm 2011-01-26 15:09:20.000000000 -0800 @@ -421,11 +421,14 @@ my %allowed_related_map = map { $_ => 1 } @$allowed_fields; my $allowed_related_cols = ($allowed_related_map{'*'}) ? [$related_source->columns] : $allowed_fields; foreach my $related_col (@{$allowed_related_cols}) { - if (defined(my $related_col_value = $related_params->{$related_col})) { + if (reftype($related_params) eq 'ARRAY') { + $values{$key} = $related_params; + } + elsif (defined(my $related_col_value = $related_params->{$related_col})) { $values{$key}{$related_col} = $related_col_value; } } } else @@ -560,11 +563,18 @@ } $object->insert; while (my ($k, $v) = each %rels) { - $object->create_related($k, $v); + if (reftype($v) eq 'ARRAY') { + foreach my $next_v ( @$v ) { + $object->create_related($k, $next_v); + } + } + else { + $object->create_related($k => $v); + } } } sub delete_objects
Sorry that it took so long, I looked at it now because I needed support for it as well. Your suggested patch didn't validate the columns of a has_many relationship and update didn't work as well. Version 2.008001 supports both. Please feel free to reopen the ticket if you have further suggestions or find a bug.