Minimal optimization of the new+prefetch codepath (no func. changes)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Row.pm
index 995b37b..cad0185 100644 (file)
@@ -118,7 +118,7 @@ with NULL as the default, and save yourself a SELECT.
 =cut
 
 ## It needs to store the new objects somewhere, and call insert on that list later when insert is called on this object. We may need an accessor for these so the user can retrieve them, if just doing ->new().
-## This only works because DBIC doesnt yet care to check whether the new_related objects have been passed all their mandatory columns
+## This only works because DBIC doesn't yet care to check whether the new_related objects have been passed all their mandatory columns
 ## When doing the later insert, we need to make sure the PKs are set.
 ## using _relationship_data in new and funky ways..
 ## check Relationship::CascadeActions and Relationship::Accessor for compat
@@ -1144,7 +1144,7 @@ sub copy {
     );
 
     my $copied = $relnames_copied->{ $rel_info->{source} } ||= {};
-    foreach my $related ($self->search_related($relname)) {
+    foreach my $related ($self->search_related($relname)->all) {
       my $id_str = join("\0", $related->id);
       next if $copied->{$id_str};
       $copied->{$id_str} = 1;
@@ -1240,17 +1240,15 @@ sub inflate_result {
       $class->throw_exception("No accessor type declared for prefetched relationship '$relname'")
         unless $relinfo->{attrs}{accessor};
 
+      my $rel_rs = $new->related_resultset($relname);
+
       my @rel_objects;
       if (
-        $prefetch->{$relname}
-          and
-        @{$prefetch->{$relname}}
+        @{ $prefetch->{$relname} || [] }
           and
         ref($prefetch->{$relname}) ne $DBIx::Class::ResultSource::RowParser::Util::null_branch_class
       ) {
 
-        my $rel_rs = $new->related_resultset($relname);
-
         if (ref $prefetch->{$relname}[0] eq 'ARRAY') {
           my $rel_rsrc = $rel_rs->result_source;
           my $rel_class = $rel_rs->result_class;
@@ -1274,7 +1272,7 @@ sub inflate_result {
         $new->{_inflated_column}{$relname} = $rel_objects[0];
       }
 
-      $new->related_resultset($relname)->set_cache(\@rel_objects);
+      $rel_rs->set_cache(\@rel_objects);
     }
   }