From: Daniel Ruoso Date: Wed, 24 Nov 2010 18:33:07 +0000 (-0300) Subject: fix the alias in the identity column X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2235a95179bf17c30a1e5364e78da5bbad23ddd6;p=dbsrgits%2FDBIx-Class-Historic.git fix the alias in the identity column When building the identity where clause, discover the current_source_alias to build the hash correctly --- diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index 1853db3..49a0ebe 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -452,8 +452,10 @@ sub related_resultset { # back, get a resultset for the current row and do a # search_related there. my $row_srcname = $source->source_name; - my %identity = map { ( $_ => $self->get_column($_) ) } $source->primary_columns; - my $row_rs = $source->schema->resultset($row_srcname)->search(\%identity); + my $base_rs = $source->schema->resultset($row_srcname); + my $alias = $base_rs->current_source_alias; + my %identity = map { ( "${alias}.${_}" => $self->get_column($_) ) } $source->primary_columns; + my $row_rs = $base_rs->search(\%identity); $row_rs->search_related($rel, $query, $attrs); @@ -574,11 +576,25 @@ sub create_related { my $rel_info = $self->result_source->relationship_info($rel); if (ref $rel_info->{cond} eq 'CODE') { my ($cond, $ext) = $rel_info->{cond}->({ self_alias => 'me', - foreign_alias => 'other', + foreign_alias => $rel, self_rowobj => $self }); $self->throw_exception("unable to set_from_related - no simplified condition available for '${rel}'") unless $ext; + + # now we need to make sure all non-identity relationship + # definitions are overriden. + my ($argref) = @_; + while ( my($col, $value) = each %$ext ) { + $col =~ s/^$rel\.//; + my $vref = ref $value; + if ($vref eq 'HASH') { + if (keys(%$value) && (keys %$value)[0] ne '=' && + !exists $argref->{$col}) { + $self->throw_exception("unable to set_from_related via complex '${rel}' condition on column(s): '${col}'") + } + } + } } my $obj = $self->search_related($rel)->create(@_); diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 124600b..e2059e5 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -2347,12 +2347,6 @@ sub _merge_with_rscond { if (keys(%$value) && (keys %$value)[0] eq '=') { $new_data{$col} = $value->{'='}; } - # in a complex condition, set_from_related needs to override - # the columns that are involved. - elsif (!exists $data->{$col} && - !exists $data->{"$alias.$col"}) { - $self->throw_exception("unable to set_from_related via complex condition on column(s): '$col'"); - } } elsif( !$vref or $vref eq 'SCALAR' or blessed($value) ) { $new_data{$col} = $value; diff --git a/t/relationship/custom.t b/t/relationship/custom.t index a7bb130..8f7ce8d 100644 --- a/t/relationship/custom.t +++ b/t/relationship/custom.t @@ -47,11 +47,11 @@ my $cds_90s_rs = $artist2->cds_90s; is_same_sql_bind($cds_90s_rs->as_query, '(SELECT cds_90s.cdid, cds_90s.artist, cds_90s.title, cds_90s.year, cds_90s.genreid,'. 'cds_90s.single_track FROM artist me JOIN cd cds_90s ON ( cds_90s.artist = me.artistid'. - ' AND ( cds_90s.year < ? AND cds_90s.year > ? ) ) WHERE ( artistid = ? ))', + ' AND ( cds_90s.year < ? AND cds_90s.year > ? ) ) WHERE ( me.artistid = ? ))', [ [ 'cds_90s.year' => 2000 ], [ 'cds_90s.year' => 1989 ], - [ 'artistid' => 5 ], + [ 'me.artistid' => 5 ], ]); my @cds_90s = $cds_90s_rs->all;