X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FRelationship%2FBase.pm;h=e04b082e0d2b18c772787cfd315eb4e1e814b053;hb=87c4e6021744dca313843a87b876c1845b72729d;hp=b662a2344cf732dfec58fcd1f731305b6caf4158;hpb=8c49f6295f3c7e07ba4bda6379f3c9f065501d7a;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index b662a23..e04b082 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -51,7 +51,7 @@ command immediately before C. An arrayref containing a list of accessors in the foreign class to proxy in the main class. If, for example, you do the following: - __PACKAGE__->might_have(bar => 'Bar', undef, { proxy => qw[/ margle /] }); + __PACKAGE__->might_have(bar => 'Bar', undef, { proxy => [ qw/margle/ ] }); Then, assuming Bar has an accessor named margle, you can do: @@ -69,8 +69,16 @@ created, which calls C for the relationship. =back +=head2 register_relationship + +=head3 Arguments: ($relname, $rel_info) + +Registers a relationship on the class + =cut +sub register_relationship { } + =head2 search_related My::Table->search_related('relname', $cond, $attrs); @@ -86,13 +94,29 @@ sub search_related { $attrs = { %{ pop(@_) } }; } my $rel_obj = $self->relationship_info($rel); - $self->throw( "No such relationship ${rel}" ) unless $rel_obj; + $self->throw_exception( "No such relationship ${rel}" ) unless $rel_obj; $attrs = { %{$rel_obj->{attrs} || {}}, %{$attrs || {}} }; - $self->throw( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1)); + $self->throw_exception( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1)); my $query = ((@_ > 1) ? {@_} : shift); my ($cond) = $self->result_source->resolve_condition($rel_obj->{cond}, $rel, $self); + if (ref $cond eq 'ARRAY') { + $cond = [ map { my %hash; + foreach my $key (keys %{$_}) { + unless ($key =~ m/\./) { + $hash{"me.$key"} = $_->{$key}; + } else { + $hash{$key} = $_->{$key}; + } + }; \%hash; } @$cond ]; + } else { + foreach my $key (keys %$cond) { + unless ($key =~ m/\./) { + $cond->{"me.$key"} = delete $cond->{$key}; + } + } + } $query = ($query ? { '-and' => [ $cond, $query ] } : $cond); #use Data::Dumper; warn Dumper($cond); #warn $rel_obj->{class}." $meth $cond ".join(', ', @{$attrs->{bind}||[]}); @@ -102,7 +126,7 @@ sub search_related { =head2 count_related - My::Table->count_related('relname', $cond, $attrs); + $obj->count_related('relname', $cond, $attrs); =cut @@ -166,20 +190,20 @@ sub find_or_create_related { sub set_from_related { my ($self, $rel, $f_obj) = @_; my $rel_obj = $self->relationship_info($rel); - $self->throw( "No such relationship ${rel}" ) unless $rel_obj; + $self->throw_exception( "No such relationship ${rel}" ) unless $rel_obj; my $cond = $rel_obj->{cond}; - $self->throw( "set_from_related can only handle a hash condition; the " + $self->throw_exception( "set_from_related can only handle a hash condition; the " ."condition for $rel is of type ".(ref $cond ? ref $cond : 'plain scalar')) unless ref $cond eq 'HASH'; my $f_class = $self->result_source->schema->class($rel_obj->{class}); - $self->throw( "Object $f_obj isn't a ".$f_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("set_from_related can't handle $key as key") + $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("set_from_related can't handle ".$cond->{$key}." as value") + $self->throw_exception("set_from_related can't handle ".$cond->{$key}." as value") unless $cond->{$key} =~ m/^self\.([^\.]+)$/; $self->set_column($1 => $val); }