[Bast-commits] r3394 - in branches/DBIx-Class-current: . lib/DBIx/Class t

matthewt at dev.catalyst.perl.org matthewt at dev.catalyst.perl.org
Tue May 29 02:05:38 GMT 2007


Author: matthewt
Date: 2007-05-29 02:05:38 +0100 (Tue, 29 May 2007)
New Revision: 3394

Added:
   branches/DBIx-Class-current/t/96multi_create.t
Modified:
   branches/DBIx-Class-current/
   branches/DBIx-Class-current/lib/DBIx/Class/Row.pm
Log:
 r35872 at cain (orig r3035):  castaway | 2007-01-18 23:57:33 +0000
 First attempt.. 
 Tests still needed for has_many rels
 



Property changes on: branches/DBIx-Class-current
___________________________________________________________________
Name: svk:merge
   - 168d5346-440b-0410-b799-f706be625ff1:/DBIx-Class-current:2207
462d4d0c-b505-0410-bf8e-ce8f877b3390:/local/bast/DBIx-Class:3159
78d5d833-3a7e-474d-aed6-cfba645156b5:/local/DBIx-Class.current:29201
78d5d833-3a7e-474d-aed6-cfba645156b5:/local/DBIx-Class.oracle8:29250
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-C3:318
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-joins:173
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-resultset:570
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/collapse_result_rewrite:3292
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/column_info_from_storage:2596
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/datetime:1716
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_compat:1855
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_unique_query_fixes:2142
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/inflate:1988
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/load_namespaces:2725
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/many_to_many:2025
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/param_bind:3015
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/re_refactor_bugfix:1944
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/reorganize_tests:1827
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset-new-refactor:1766
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_2_electric_boogaloo:2175
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_cleanup:2102
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/source-handle:2975
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/sqlt_tests_refactor:2043
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/storage_exceptions:2617
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/versioning:2930
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/trunk/DBIx-Class:3355
   + 168d5346-440b-0410-b799-f706be625ff1:/DBIx-Class-current:2207
462d4d0c-b505-0410-bf8e-ce8f877b3390:/local/bast/DBIx-Class:3159
78d5d833-3a7e-474d-aed6-cfba645156b5:/local/DBIx-Class.current:29201
78d5d833-3a7e-474d-aed6-cfba645156b5:/local/DBIx-Class.oracle8:29250
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-C3:318
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-joins:173
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-resultset:570
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/bulk_create:3035
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/collapse_result_rewrite:3292
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/column_info_from_storage:2596
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/datetime:1716
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_compat:1855
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_unique_query_fixes:2142
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/inflate:1988
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/load_namespaces:2725
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/many_to_many:2025
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/param_bind:3015
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/re_refactor_bugfix:1944
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/reorganize_tests:1827
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset-new-refactor:1766
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_2_electric_boogaloo:2175
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_cleanup:2102
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/source-handle:2975
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/sqlt_tests_refactor:2043
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/storage_exceptions:2617
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/versioning:2930
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/trunk/DBIx-Class:3355

Modified: branches/DBIx-Class-current/lib/DBIx/Class/Row.pm
===================================================================
--- branches/DBIx-Class-current/lib/DBIx/Class/Row.pm	2007-05-29 00:52:25 UTC (rev 3393)
+++ branches/DBIx-Class-current/lib/DBIx/Class/Row.pm	2007-05-29 01:05:38 UTC (rev 3394)
@@ -5,6 +5,7 @@
 
 use base qw/DBIx::Class/;
 use Carp::Clan qw/^DBIx::Class/;
+use Scalar::Util ();
 
 __PACKAGE__->mk_group_accessors('simple' => qw/_source_handle/);
 
@@ -27,8 +28,22 @@
 
 Creates a new row object from column => value mappings passed as a hash ref
 
+Passing an object, or an arrayref of objects as a value will call
+L<DBIx::Class::Relationship::Base/set_from_related> for you. When
+passed a hashref or an arrayref of hashrefs as the value, these will
+be turned into objects via new_related, and treated as if you had
+passed objects.
+
 =cut
 
+## NB (JER) - this assumes set_from_related can cope with multi-rels
+## 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
+## 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
+## tests!
+
 sub new {
   my ($class, $attrs) = @_;
   $class = ref $class if ref $class;
@@ -54,14 +69,39 @@
         if ($info && $info->{attrs}{accessor}
           && $info->{attrs}{accessor} eq 'single')
         {
+          my $rel_obj = $attrs->{$key};
+          $new->{_rel_in_storage} = 1;
+          if(!Scalar::Util::blessed($rel_obj)) {
+            $rel_obj = $new->new_related($key, $rel_obj);
+            $new->{_rel_in_storage} = 0;
+          }
           $new->set_from_related($key, $attrs->{$key});        
           $related->{$key} = $attrs->{$key};
           next;
-        }
-        elsif ($class->has_column($key)
+        } elsif ($info && $info->{attrs}{accessor}
+            && $info->{attrs}{accessor} eq 'multi'
+            && ref $attrs->{$key} eq 'ARRAY') {
+            my $others = $attrs->{$key};
+            $new->{_rel_in_storage} = 1;
+            foreach my $rel_obj (@$others) {
+              if(!Scalar::Util::blessed($rel_obj)) {
+                $rel_obj = $new->new_related($key, $rel_obj);
+                $new->{_rel_in_storage} = 0;
+              }
+            }
+            $new->set_from_related($key, $others);
+            $related->{$key} = $attrs->{$key};
+        } elsif ($class->has_column($key)
           && exists $class->column_info($key)->{_inflate_info})
         {
-          $inflated->{$key} = $attrs->{$key};
+          ## 'filter' should disappear and get merged in with 'single' above!
+          my $rel_obj = $attrs->{$key};
+          $new->{_rel_in_storage} = 1;
+          if(!Scalar::Util::blessed($rel_obj)) {
+            $rel_obj = $new->new_related($key, $rel_obj);
+            $new->{_rel_in_storage} = 0;
+          }
+          $inflated->{$key} = $rel_obj;
           next;
         }
       }
@@ -97,8 +137,38 @@
     if $self->can('result_source_instance');
   $self->throw_exception("No result_source set on this object; can't insert")
     unless $source;
+  #use Data::Dumper; warn Dumper($self);
+  # Check if we stored uninserted relobjs here in new()
+  $source->storage->txn_begin if(!$self->{_rel_in_storage});
 
-  $source->storage->insert($source, { $self->get_columns });
+  my %related_stuff = (%{$self->{_relationship_data} || {}}, 
+                       %{$self->{_inflated_column} || {}});
+  ## Should all be in relationship_data, but we need to get rid of the
+  ## 'filter' reltype..
+  foreach my $relname (keys %related_stuff) {
+    my $relobj = $related_stuff{$relname};
+    if(ref $relobj ne 'ARRAY') {
+      $relobj->insert() if(!$relobj->in_storage);
+      $self->set_from_related($relname, $relobj);
+    }
+  }
+
+  $source->storage->insert($source->from, { $self->get_columns });
+
+  foreach my $relname (keys %related_stuff) {
+    my $relobj = $related_stuff{$relname};
+    if(ref $relobj eq 'ARRAY') {
+      foreach my $obj (@$relobj) {
+        my $info = $self->relationship_info($relname);
+        ## What about multi-col FKs ?
+        my $key = $1 if($info && (keys %{$info->{cond}})[0] =~ /^foreign\.(\w+)/);
+        $obj->set_from_related($key, $self);
+        $obj->insert() if(!$obj->in_storage);
+      }
+    }
+  }
+  $source->storage->txn_commit if(!$self->{_rel_in_storage});
+
   $self->in_storage(1);
   $self->{_dirty_columns} = {};
   $self->{related_resultsets} = {};

Added: branches/DBIx-Class-current/t/96multi_create.t
===================================================================
--- branches/DBIx-Class-current/t/96multi_create.t	                        (rev 0)
+++ branches/DBIx-Class-current/t/96multi_create.t	2007-05-29 01:05:38 UTC (rev 3394)
@@ -0,0 +1,25 @@
+use strict;
+use warnings;  
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+
+my $schema = DBICTest->init_schema();
+
+plan tests => 1;
+
+my $artist = $schema->resultset('Artist')->create({ name => 'Fred 1'});
+
+my $cd = $schema->resultset('CD')->create({ artist => $artist,
+                                            title => 'Some CD',
+                                            year => 1996
+                                          });
+
+my $cd2 = $schema->resultset('CD')->create({ artist => 
+                                   { name => 'Fred Bloggs' },
+                                   title => 'Some CD',
+                                   year => 1996
+                                 });
+
+is(ref $cd->artist, 'DBICTest::Artist', 'Created CD and Artist object');




More information about the Bast-commits mailing list