From: Jess Robinson Date: Tue, 6 Feb 2007 19:38:35 +0000 (+0000) Subject: Add PK::Auto to Row/insert for now, could be prettier X-Git-Tag: v0.08010~150^2~51^2~21 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ac8e89d7b8106edf37bd4806fa6a052dba0ab469;p=dbsrgits%2FDBIx-Class.git Add PK::Auto to Row/insert for now, could be prettier --- diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index 8409165..c865642 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -317,12 +317,16 @@ sub update_or_create_related { =head2 set_from_related $book->set_from_related('author', $author_obj); + $book->author($author_obj); ## same thing Set column values on the current object, using related values from the given related object. This is used to associate previously separate objects, for example, to set the correct author for a book, find the Author object, then call set_from_related on the book. +This is called internally when you pass existing objects as values to +L, or pass an object to a belongs_to acessor. + The columns are only set in the local copy of the object, call L to set them in the storage. diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index 0fde913..dc1312d 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -76,7 +76,7 @@ sub new { } elsif ($info && $info->{attrs}{accessor} && $info->{attrs}{accessor} eq 'multi' && ref $attrs->{$key} eq 'ARRAY') { - my $others = $attrs->{$key}; + my $others = delete $attrs->{$key}; $new->{_rel_in_storage} = 1; foreach my $rel_obj (@$others) { if(!Scalar::Util::blessed($rel_obj)) { @@ -84,8 +84,9 @@ sub new { $new->{_rel_in_storage} = 0; } } - $new->set_from_related($key, $others); - $related->{$key} = $attrs->{$key}; +# $new->set_from_related($key, $others); + $related->{$key} = $others; + next; } elsif ($class->has_column($key) && exists $class->column_info($key)->{_inflate_info}) { @@ -151,7 +152,22 @@ sub insert { } } - $source->storage->insert($source->from, { $self->get_columns }); + $source->storage->insert($source, { $self->get_columns }); + + ## PK::Auto + my ($pri, $too_many) = grep { !defined $self->get_column($_) || + ref($self->get_column($_)) eq 'SCALAR'} $self->primary_columns; + if(defined $pri) { + $self->throw_exception( "More than one possible key found for auto-inc on ".ref $self ) + if defined $too_many; + + my $storage = $self->result_source->storage; + $self->throw_exception( "Missing primary key but Storage doesn't support last_insert_id" ) + unless $storage->can('last_insert_id'); + my $id = $storage->last_insert_id($self->result_source,$pri); + $self->throw_exception( "Can't get last insert id" ) unless $id; + $self->store_column($pri => $id); + } foreach my $relname (keys %related_stuff) { my $relobj = $related_stuff{$relname}; diff --git a/t/96multi_create.t b/t/96multi_create.t index 205ba4f..f7f5602 100644 --- a/t/96multi_create.t +++ b/t/96multi_create.t @@ -7,14 +7,7 @@ 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 - }); +plan tests => 4; my $cd2 = $schema->resultset('CD')->create({ artist => { name => 'Fred Bloggs' }, @@ -22,4 +15,15 @@ my $cd2 = $schema->resultset('CD')->create({ artist => year => 1996 }); -is(ref $cd->artist, 'DBICTest::Artist', 'Created CD and Artist object'); +is(ref $cd2->artist, 'DBICTest::Artist', 'Created CD and Artist object'); +is($cd2->artist->name, 'Fred Bloggs', 'Artist created correctly'); + +my $artist = $schema->resultset('Artist')->create({ name => 'Fred 2', + cds => [ + { title => 'Music to code by', + year => 2007, + }, + ], + }); +is(ref $artist->cds->first, 'DBICTest::CD', 'Created Artist with CDs'); +is($artist->cds->first->title, 'Music to code by', 'CD created correctly');