X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FRelationship%2FAccessor.pm;h=6ec2f258cd0c555280b7f8be829a13a3100a3ded;hb=6ffb5be522e752ea1ad2a99d36648535fe30a43b;hp=e98c9a21375e7765397282ce356a847e28a85fa1;hpb=bd3d5a5e91e959cc7f4fcf0d10a15d65412115b9;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Relationship/Accessor.pm b/lib/DBIx/Class/Relationship/Accessor.pm index e98c9a2..6ec2f25 100644 --- a/lib/DBIx/Class/Relationship/Accessor.pm +++ b/lib/DBIx/Class/Relationship/Accessor.pm @@ -3,6 +3,8 @@ package # hide from PAUSE use strict; use warnings; +use Sub::Name (); +use Class::Inspector (); sub register_relationship { my ($class, $rel, $info) = @_; @@ -16,6 +18,7 @@ sub add_relationship_accessor { my ($class, $rel, $acc_type) = @_; my %meth; if ($acc_type eq 'single') { + my $rel_info = $class->relationship_info($rel); $meth{$rel} = sub { my $self = shift; if (@_) { @@ -24,6 +27,13 @@ sub add_relationship_accessor { } elsif (exists $self->{_relationship_data}{$rel}) { return $self->{_relationship_data}{$rel}; } else { + my $cond = $self->result_source->resolve_condition( + $rel_info->{cond}, $rel, $self + ); + if ($rel_info->{attrs}->{undef_on_null_fk}){ + return unless ref($cond) eq 'HASH'; + return if grep { not defined } values %$cond; + } my $val = $self->find_related($rel, {}, {}); return unless $val; return $self->{_relationship_data}{$rel} = $val; @@ -36,7 +46,7 @@ sub add_relationship_accessor { $class->inflate_column($rel, { inflate => sub { my ($val, $self) = @_; - return $self->find_or_create_related($rel, {}, {}); + return $self->find_or_new_related($rel, {}, {}); }, deflate => sub { my ($val, $self) = @_; @@ -57,45 +67,10 @@ sub add_relationship_accessor { no strict 'refs'; no warnings 'redefine'; foreach my $meth (keys %meth) { - *{"${class}::${meth}"} = $meth{$meth}; - } - } -} - -sub new { - my ($class, $attrs, @rest) = @_; - my ($related, $info); - foreach my $key (keys %{$attrs||{}}) { - next unless $info = $class->relationship_info($key); - $related->{$key} = delete $attrs->{$key} - if ref $attrs->{$key} - && $info->{attrs}{accessor} - && $info->{attrs}{accessor} eq 'single'; - } - my $obj = $class->next::method($attrs, @rest); - if ($related) { - $obj->{_relationship_data} = $related; - foreach my $rel (keys %$related) { - $obj->set_from_related($rel, $related->{$rel}); - } - } - return $obj; -} - -sub update { - my ($obj, $attrs, @rest) = @_; - $attrs = { %$attrs} if $attrs; # Take copy so we dont change passed hashref - my $info; - foreach my $key (keys %{$attrs||{}}) { - next unless $info = $obj->relationship_info($key); - if (ref $attrs->{$key} && $info->{attrs}{accessor} - && $info->{attrs}{accessor} eq 'single') { - my $rel = delete $attrs->{$key}; - $obj->set_from_related($key => $rel); - $obj->{_relationship_data}{$key} = $rel; + my $name = join '::', $class, $meth; + *$name = Sub::Name::subname($name, $meth{$meth}); } } - return $obj->next::method($attrs, @rest); } 1;