Allow accessors type single to return defined but false objects (needed for null...
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / Accessor.pm
CommitLineData
75d07914 1package # hide from PAUSE
c0e7b4e5 2 DBIx::Class::Relationship::Accessor;
4a07648a 3
4use strict;
5use warnings;
ddc0a6c8 6use Sub::Name ();
7use Class::Inspector ();
4a07648a 8
71e65b39 9sub register_relationship {
10 my ($class, $rel, $info) = @_;
11 if (my $acc_type = $info->{attrs}{accessor}) {
223b8fe3 12 $class->add_relationship_accessor($rel => $acc_type);
4a07648a 13 }
71e65b39 14 $class->next::method($rel => $info);
4a07648a 15}
16
223b8fe3 17sub add_relationship_accessor {
4a07648a 18 my ($class, $rel, $acc_type) = @_;
19 my %meth;
20 if ($acc_type eq 'single') {
7a7e8718 21 my $rel_info = $class->relationship_info($rel);
4a07648a 22 $meth{$rel} = sub {
23 my $self = shift;
24 if (@_) {
25 $self->set_from_related($rel, @_);
26 return $self->{_relationship_data}{$rel} = $_[0];
27 } elsif (exists $self->{_relationship_data}{$rel}) {
28 return $self->{_relationship_data}{$rel};
29 } else {
84a1c93f 30 my $cond = $self->result_source->resolve_condition(
7a7e8718 31 $rel_info->{cond}, $rel, $self
84a1c93f 32 );
7a7e8718 33 if ($rel_info->{attrs}->{undef_on_null_fk}){
6ffb5be5 34 return unless ref($cond) eq 'HASH';
7a7e8718 35 return if grep { not defined } values %$cond;
36 }
1a14aa3f 37 my $val = $self->find_related($rel, {}, {});
cde1c1e1 38
39 # this really should have been:
40 # return $val unless $val
41 # however someone might already be relying on return() as in:
42 # my @things = map { $_->might_have_acc } ($rs->all)
43 # thus keeping the quirky behavior
44 return unless defined $val;
45
b28cc0ba 46 return $self->{_relationship_data}{$rel} = $val;
4a07648a 47 }
48 };
49 } elsif ($acc_type eq 'filter') {
701da8c4 50 $class->throw_exception("No such column $rel to filter")
103647d5 51 unless $class->has_column($rel);
4685e006 52 my $f_class = $class->relationship_info($rel)->{class};
4a07648a 53 $class->inflate_column($rel,
54 { inflate => sub {
55 my ($val, $self) = @_;
e60dc79f 56 return $self->find_or_new_related($rel, {}, {});
4a07648a 57 },
58 deflate => sub {
59 my ($val, $self) = @_;
701da8c4 60 $self->throw_exception("$val isn't a $f_class") unless $val->isa($f_class);
4a07648a 61 return ($val->_ident_values)[0];
62 # WARNING: probably breaks for multi-pri sometimes. FIXME
63 }
64 }
65 );
66 } elsif ($acc_type eq 'multi') {
67 $meth{$rel} = sub { shift->search_related($rel, @_) };
5b89a768 68 $meth{"${rel}_rs"} = sub { shift->search_related_rs($rel, @_) };
4a07648a 69 $meth{"add_to_${rel}"} = sub { shift->create_related($rel, @_); };
70 } else {
701da8c4 71 $class->throw_exception("No such relationship accessor type $acc_type");
4a07648a 72 }
73 {
74 no strict 'refs';
75 no warnings 'redefine';
76 foreach my $meth (keys %meth) {
ddc0a6c8 77 my $name = join '::', $class, $meth;
78 *$name = Sub::Name::subname($name, $meth{$meth});
4a07648a 79 }
80 }
81}
82
831;