=back
-Pass an arrayref of hashrefs. Each hashref should be a structure suitable for
-submitting to a $resultset->create(...) method.
+Accepts either an arrayref of hashrefs or alternatively an arrayref of arrayrefs.
+For the arrayref of hashrefs style each hashref should be a structure suitable
+forsubmitting to a $resultset->create(...) method.
In void context, C<insert_bulk> in L<DBIx::Class::Storage::DBI> is used
to insert the data, as this is a faster method.
print $ArtistOne->name; ## response is 'Artist One'
print $ArtistThree->cds->count ## reponse is '2'
-
+
+For the arrayref of arrayrefs style, the first element should be a list of the
+fieldsnames to which the remaining elements are rows being inserted. For
+example:
+
+ $Arstist_rs->populate([
+ [qw/artistid name/],
+ [100, 'A Formally Unknown Singer'],
+ [101, 'A singer that jumped the shark two albums ago'],
+ [102, 'An actually cool singer.'],
+ ]);
+
Please note an important effect on your data when choosing between void and
wantarray context. Since void context goes straight to C<insert_bulk> in
L<DBIx::Class::Storage::DBI> this will skip any component that is overriding
=cut
sub populate {
- my ($self, $data) = @_;
+ my $self = shift @_;
+ my $data = ref $_[0][0] eq 'HASH'
+ ? $_[0] : ref $_[0][0] eq 'ARRAY' ? $self->_normalize_populate_args($_[0]) :
+ $self->throw_exception('Populate expects an arrayref of hashes or arrayref of arrayrefs');
if(defined wantarray) {
my @created;
}
}
+=head2 _normalize_populate_args ($args)
+
+Private method used by L</populate> to normalize it's incoming arguments. Factored
+out in case you want to subclass and accept new argument structures to the
+L</populate> method.
+
+=cut
+
+sub _normalize_populate_args {
+ my ($self, $data) = @_;
+ my @names = @{shift(@$data)};
+ my @results_to_create;
+ foreach my $datum (@$data) {
+ my %result_to_create;
+ foreach my $index (0..$#names) {
+ $result_to_create{$names[$index]} = $$datum[$index];
+ }
+ push @results_to_create, \%result_to_create;
+ }
+ return \@results_to_create;
+}
+
=head2 pager
=over 4
sub populate {
my ($self, $name, $data) = @_;
- my $rs = $self->resultset($name);
- my @names = @{shift(@$data)};
- if(defined wantarray) {
- my @created;
- foreach my $item (@$data) {
- my %create;
- @create{@names} = @$item;
- push(@created, $rs->create(\%create));
+ if(my $rs = $self->resultset($name)) {
+ if(defined wantarray) {
+ return $rs->populate($data);
+ } else {
+ $rs->populate($data);
}
- return @created;
- }
- my @results_to_create;
- foreach my $datum (@$data) {
- my %result_to_create;
- foreach my $index (0..$#names) {
- $result_to_create{$names[$index]} = $$datum[$index];
- }
- push @results_to_create, \%result_to_create;
+ } else {
+ $self->throw_exception("$name is not a resultset");
}
- $rs->populate(\@results_to_create);
}
=head2 connection
use lib qw(t/lib);
use DBICTest;
-plan tests => 134;
+plan tests => 142;
## ----------------------------------------------------------------------------
ok( $cd2->title eq "VOID_Yet More Tweeny-Pop crap", "Got Expected CD Title");
}
+}
+
+ARRAYREF_OF_ARRAYREF_STYLE: {
+ $art_rs->populate([
+ [qw/artistid name/],
+ [1000, 'A Formally Unknown Singer'],
+ [1001, 'A singer that jumped the shark two albums ago'],
+ [1002, 'An actually cool singer.'],
+ ]);
+
+ ok my $unknown = $art_rs->find(1000), "got Unknown";
+ ok my $jumped = $art_rs->find(1001), "got Jumped";
+ ok my $cool = $art_rs->find(1002), "got Cool";
+
+ is $unknown->name, 'A Formally Unknown Singer', 'Correct Name';
+ is $jumped->name, 'A singer that jumped the shark two albums ago', 'Correct Name';
+ is $cool->name, 'An actually cool singer.', 'Correct Name';
+
+ my ($cooler, $lamer) = $art_rs->populate([
+ [qw/artistid name/],
+ [1003, 'Cooler'],
+ [1004, 'Lamer'],
+ ]);
+
+ is $cooler->name, 'Cooler', 'Correct Name';
+ is $lamer->name, 'Lamer', 'Correct Name';
}
\ No newline at end of file