X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FRelationship%2FBase.pm;h=2bbbb032c79969a9c343016c8481fc693a9c0294;hb=fea3d0452969e98afc0296ba80f90a91ab7dc1be;hp=8f41771fef9010ee168b6d9820227d96068594ed;hpb=130c64391b48bae9eb374e931c7d6c308625bf6b;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index 8f41771..2bbbb03 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -26,14 +26,49 @@ on searches. __PACKAGE__->add_relationship('relname', 'Foreign::Class', $cond, $attrs); The condition needs to be an SQL::Abstract-style representation of the -join between the tables - for example if you're creating a rel from Foo to Bar +join between the tables. For example, if you're creating a rel from Foo to Bar, { 'foreign.foo_id' => 'self.id' } -will result in a JOIN clause like +will result in the JOIN clause foo me JOIN bar bar ON bar.foo_id = me.id +You can specify as many foreign => self mappings as necessary. + +Valid attributes are as follows: + +=over 4 + +=item join_type + +Explicitly specifies the type of join to use in the relationship. Any SQL +join type is valid, e.g. C or C. It will be placed in the SQL +command immediately before C. + +=item proxy + +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 /] }); + +Then, assuming Bar has an accessor named margle, you can do: + + my $obj = Foo->find(1); + $obj->margle(10); # set margle; Bar object is created if it doesn't exist + +=item accessor + +Specifies the type of accessor that should be created for the relationship. +Valid values are C (for when there is only a single related object), +C (when there can be many), and C (for when there is a single +related object, but you also want the relationship accessor to double as +a column accessor). For C accessors, an add_to_* method is also +created, which calls C for the relationship. + +=back + =cut sub add_relationship { @@ -104,13 +139,17 @@ sub resolve_condition { } sub _cond_key { - my ($self, $attrs, $key) = @_; + my ($self, $attrs, $key, $alias) = @_; my $action = $attrs->{_action} || ''; if ($action eq 'convert') { unless ($key =~ s/^foreign\.//) { $self->throw("Unable to convert relationship to WHERE clause: invalid key ${key}"); } - return $key; + if (defined (my $alias = $attrs->{_aliases}{foreign})) { + return "${alias}.${key}"; + } else { + return $key; + } } elsif ($action eq 'join') { return $key unless $key =~ /\./; my ($type, $field) = split(/\./, $key); @@ -163,23 +202,6 @@ sub _cond_value { sub search_related { my $self = shift; - return $self->_query_related('search', @_); -} - -=head2 count_related - - My::Table->count_related('relname', $cond, $attrs); - -=cut - -sub count_related { - my $self = shift; - return $self->_query_related('count', @_); -} - -sub _query_related { - my $self = shift; - my $meth = shift; my $rel = shift; my $attrs = { }; if (@_ > 1 && ref $_[$#_] eq 'HASH') { @@ -200,7 +222,18 @@ sub _query_related { #warn $rel_obj->{class}." $meth $cond ".join(', ', @{$attrs->{bind}||[]}); delete $attrs->{_action}; return $self->resolve_class($rel_obj->{class} - )->$meth($query, $attrs); + )->search($query, $attrs); +} + +=head2 count_related + + My::Table->count_related('relname', $cond, $attrs); + +=cut + +sub count_related { + my $self = shift; + return $self->search_related(@_)->count; } =head2 create_related @@ -211,7 +244,8 @@ sub _query_related { sub create_related { my $class = shift; - return $class->new_related(@_)->insert; + my $rel = shift; + return $class->search_related($rel)->create(@_); } =head2 new_related @@ -222,20 +256,7 @@ sub create_related { sub new_related { my ($self, $rel, $values, $attrs) = @_; - $self->throw( "Can't call new_related as class method" ) - unless ref $self; - $self->throw( "new_related needs a hash" ) - unless (ref $values eq 'HASH'); - my $rel_obj = $self->_relationships->{$rel}; - $self->throw( "No such relationship ${rel}" ) unless $rel_obj; - $self->throw( "Can't abstract implicit create for ${rel}, condition not a hash" ) - unless ref $rel_obj->{cond} eq 'HASH'; - $attrs = { %{$rel_obj->{attrs}}, %{$attrs || {}}, _action => 'convert' }; - - my %fields = %{$self->resolve_condition($rel_obj->{cond},$attrs)}; - $fields{$_} = $values->{$_} for keys %$values; - - return $self->resolve_class($rel_obj->{class})->new(\%fields); + return $self->search_related($rel)->new($values, $attrs); } =head2 find_related @@ -247,17 +268,7 @@ sub new_related { sub find_related { my $self = shift; my $rel = shift; - my $rel_obj = $self->_relationships->{$rel}; - $self->throw( "No such relationship ${rel}" ) unless $rel_obj; - my ($cond) = $self->resolve_condition($rel_obj->{cond}, { _action => 'convert' }); - $self->throw( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1)); - my $attrs = { }; - if (@_ > 1 && ref $_[$#_] eq 'HASH') { - $attrs = { %{ pop(@_) } }; - } - my $query = ((@_ > 1) ? {@_} : shift); - $query = ($query ? { '-and' => [ $cond, $query ] } : $cond); - return $self->resolve_class($rel_obj->{class})->find($query); + return $self->search_related($rel)->find(@_); } =head2 find_or_create_related