From: Matt S Trout Date: Tue, 14 Feb 2006 15:43:35 +0000 (+0000) Subject: Improvements to copy() and set_from_related() code X-Git-Tag: v0.06000~61^2~32 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fde6e28e54ac4ca5ec0f0616f7e96eea20ef83dd;p=dbsrgits%2FDBIx-Class.git Improvements to copy() and set_from_related() code --- diff --git a/Changes b/Changes index e200673..de60b28 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ Revision history for DBIx::Class + - clean up set_from_related + - made copy() automatically null out auto-inc columns - Another fix for count with scalar group_by. 0.05005 2006-02-13 21:24:51 diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index b10c687..7e6542d 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -196,15 +196,9 @@ sub set_from_related { my $f_class = $self->result_source->schema->class($rel_obj->{class}); $self->throw_exception( "Object $f_obj isn't a ".$f_class ) unless $f_obj->isa($f_class); - foreach my $key (keys %$cond) { - next if ref $cond->{$key}; # Skip literals and complex conditions - $self->throw_exception("set_from_related can't handle $key as key") - unless $key =~ m/^foreign\.([^\.]+)$/; - my $val = $f_obj->get_column($1); - $self->throw_exception("set_from_related can't handle ".$cond->{$key}." as value") - unless $cond->{$key} =~ m/^self\.([^\.]+)$/; - $self->set_column($1 => $val); - } + $self->set_columns( + $self->result_source->resolve_condition( + $rel_obj->{cond}, $f_obj, $rel)); return 1; } diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index 7270b5f..2e75f32 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -405,6 +405,8 @@ sub resolve_condition { #warn "$self $k $for $v"; $ret{$k} = $for->get_column($v); #warn %ret; + } elsif (ref $as) { # reverse object + $ret{$v} = $as->get_column($k); } else { $ret{"${as}.${k}"} = "${for}.${v}"; } diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index bf4a526..9128599 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -236,7 +236,12 @@ Inserts a new row with the specified changes. sub copy { my ($self, $changes) = @_; - my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self); + my $col_data = { %{$self->{_column_data}} }; + foreach my $col (keys %$col_data) { + delete $col_data->{$col} + if $self->result_source->column_info($col)->{is_auto_increment}; + } + my $new = bless({ _column_data => $col_data }, ref $self); $new->set_column($_ => $changes->{$_}) for keys %$changes; return $new->insert; }