From: Peter Rabbitson Date: Tue, 11 Aug 2009 18:00:11 +0000 (+0000) Subject: Fix an obscure regression when inserting an object with a serialize-deflating column set X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=31c3800e8b5f46df64da3c9671a06a0727897f05;p=dbsrgits%2FDBIx-Class-Historic.git Fix an obscure regression when inserting an object with a serialize-deflating column set --- diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index 89f1de3..5d6285f 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -354,18 +354,17 @@ sub insert { $self->{related_resultsets} = {}; foreach my $relname (keys %related_stuff) { - my $rel_obj = $related_stuff{$relname}; - my @cands; - if (Scalar::Util::blessed($rel_obj) - && $rel_obj->isa('DBIx::Class::Row')) - { - @cands = ($rel_obj); - } - elsif (ref $rel_obj eq 'ARRAY') { - @cands = @$rel_obj; - } + next unless $source->has_relationship ($relname); + + my @cands = ref $related_stuff{$relname} eq 'ARRAY' + ? @{$related_stuff{$relname}} + : $related_stuff{$relname} + ; - if (@cands) { + if (@cands + && Scalar::Util::blessed($cands[0]) + && $cands[0]->isa('DBIx::Class::Row') + ) { my $reverse = $source->reverse_relationship_info($relname); foreach my $obj (@cands) { $obj->set_from_related($_, $self) for keys %$reverse; diff --git a/t/inflate/serialize.t b/t/inflate/serialize.t index 1564798..e9b51df 100644 --- a/t/inflate/serialize.t +++ b/t/inflate/serialize.t @@ -71,10 +71,17 @@ is_deeply($inflated, $struct_hash, 'inflated hash matches original'); $object = $rs->create( { serialized => '', } ); -eval { $object->set_inflated_column('serialized', $struct_hash) }; -ok(!$@, 'set_inflated_column to a hashref'); +$object->set_inflated_column('serialized', $struct_hash); is_deeply($object->serialized, $struct_hash, 'inflated hash matches original'); +$object = $rs->new({}); +$object->serialized ($struct_hash); +$object->insert; +is_deeply ( + $rs->find ({id => $object->id})->serialized, + $struct_hash, + 'new/insert works', +); #====== testing arrayref serialization @@ -82,8 +89,16 @@ ok($object->update( { serialized => $struct_array } ), 'arrayref deflation'); ok($inflated = $object->serialized, 'arrayref inflation'); is_deeply($inflated, $struct_array, 'inflated array matches original'); +$object = $rs->new({}); +$object->serialized ($struct_array); +$object->insert; +is_deeply ( + $rs->find ({id => $object->id})->serialized, + $struct_array, + 'new/insert works', +); -#===== make sure make_column_dirty ineracts reasonably with inflation +#===== make sure make_column_dirty interacts reasonably with inflation $object = $rs->first; $object->update ({serialized => { x => 'y'}}); diff --git a/t/lib/DBICTest/Schema/Serialized.pm b/t/lib/DBICTest/Schema/Serialized.pm index 92c210f..d7737bd 100644 --- a/t/lib/DBICTest/Schema/Serialized.pm +++ b/t/lib/DBICTest/Schema/Serialized.pm @@ -5,7 +5,7 @@ use base qw/DBICTest::BaseResult/; __PACKAGE__->table('serialized'); __PACKAGE__->add_columns( - 'id' => { data_type => 'integer' }, + 'id' => { data_type => 'integer', is_auto_increment => 1 }, 'serialized' => { data_type => 'text' }, ); __PACKAGE__->set_primary_key('id');