From: Jess Robinson Date: Sat, 20 Jan 2007 22:31:19 +0000 (+0000) Subject: Merge 'DBIx-Class-current' into 'bulk_create' X-Git-Tag: v0.08010~150^2~51^2~22 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4494d3c6fa2e336fe58aa77450db1576989b56ff;hp=9b0d64fc7d7f6e84e31b6a99e57d3c6db8641764;p=dbsrgits%2FDBIx-Class.git Merge 'DBIx-Class-current' into 'bulk_create' r3022@lilith (orig r3020): matthewt | 2007-01-17 01:24:13 +0000 r33849@cain (orig r2908): jnapiorkowski | 2006-11-18 00:48:30 +0000 r3023@lilith (orig r3021): matthewt | 2007-01-17 01:25:24 +0000 r33995@cain (orig r2910): jnapiorkowski | 2006-11-20 05:01:56 +0000 changed storage->insert|update|delete to accept the source object directly and to get the attributes for the columns in storage::DBI and not elsewhere. Cleaned up the tests. Still getting attribute info from the table schemas r3024@lilith (orig r3022): matthewt | 2007-01-17 01:25:37 +0000 r34195@cain (orig r2954): jnapiorkowski | 2006-12-01 04:35:26 +0000 moved bind attributes to DBI.pm/DBI/Pg.pm r3025@lilith (orig r3023): matthewt | 2007-01-17 01:25:42 +0000 r35326@cain (orig r2968): jnapiorkowski | 2006-12-13 01:04:55 +0000 updated bulk insert to handle bind_param_array, created some basic tests r3026@lilith (orig r3024): matthewt | 2007-01-17 01:25:47 +0000 r3027@lilith (orig r3025): matthewt | 2007-01-17 01:26:29 +0000 r35669@cain (orig r2989): matthewt | 2006-12-30 20:18:22 +0000 fixup external bind params r3028@lilith (orig r3026): matthewt | 2007-01-17 01:26:43 +0000 r35821@cain (orig r3012): jnapiorkowski | 2007-01-10 21:08:36 +0000 Merge from current and fixed issue when a column has multiple bind values. r3029@lilith (orig r3027): matthewt | 2007-01-17 01:26:47 +0000 r35822@cain (orig r3013): jnapiorkowski | 2007-01-10 21:09:07 +0000 documentation updates r3030@lilith (orig r3028): matthewt | 2007-01-17 01:26:49 +0000 r35823@cain (orig r3014): jnapiorkowski | 2007-01-10 21:43:19 +0000 some refactoring to reduce cut and paste work r3031@lilith (orig r3029): matthewt | 2007-01-17 01:26:52 +0000 r35824@cain (orig r3015): jnapiorkowski | 2007-01-10 22:11:21 +0000 additional refactoring to better handle source objects r3032@lilith (orig r3030): matthewt | 2007-01-17 01:35:23 +0000 arguably slightly less broken r3036@lilith (orig r3034): matthewt | 2007-01-17 01:42:25 +0000 Changes note for param_bind since the authors forgot r3038@lilith (orig r3036): victori | 2007-01-19 03:04:48 +0000 committing first version of filecolumn r3039@lilith (orig r3037): victori | 2007-01-20 19:46:33 +0000 minor update to FileColumn. I added a callback method which can be used by FileColumn::Thumbnail r3044@lilith (orig r3042): matthewt | 2007-01-20 21:59:31 +0000 fix Sweet pager test --- diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index d66beb1..0fde913 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -5,6 +5,7 @@ use warnings; 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 @@ derived from L objects. 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 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, $source) = @_; $class = ref $class if ref $class; @@ -49,14 +64,39 @@ sub new { 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; } } @@ -96,7 +136,37 @@ sub insert { $self->throw_exception("No result_source set on this object; can't insert") unless $source; - $source->storage->insert($source, { $self->get_columns }); + # Check if we stored uninserted relobjs here in new() + $source->storage->txn_begin if(!$self->{_rel_in_storage}); + + 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} = {}; diff --git a/t/96multi_create.t b/t/96multi_create.t new file mode 100644 index 0000000..205ba4f --- /dev/null +++ b/t/96multi_create.t @@ -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');