Commit | Line | Data |
75d07914 |
1 | package # hide from PAUSE |
c0e7b4e5 |
2 | DBIx::Class::Relationship::Accessor; |
4a07648a |
3 | |
4 | use strict; |
5 | use warnings; |
6dd43920 |
6 | use Sub::Name; |
7 | use DBIx::Class::Carp; |
a9da9b6a |
8 | use DBIx::Class::_Util 'fail_on_internal_wantarray'; |
6dd43920 |
9 | use namespace::clean; |
4a07648a |
10 | |
8273e845 |
11 | our %_pod_inherit_config = |
044e70c7 |
12 | ( |
13 | class_map => { 'DBIx::Class::Relationship::Accessor' => 'DBIx::Class::Relationship' } |
14 | ); |
15 | |
71e65b39 |
16 | sub register_relationship { |
17 | my ($class, $rel, $info) = @_; |
18 | if (my $acc_type = $info->{attrs}{accessor}) { |
223b8fe3 |
19 | $class->add_relationship_accessor($rel => $acc_type); |
4a07648a |
20 | } |
71e65b39 |
21 | $class->next::method($rel => $info); |
4a07648a |
22 | } |
23 | |
223b8fe3 |
24 | sub add_relationship_accessor { |
4a07648a |
25 | my ($class, $rel, $acc_type) = @_; |
6243d42b |
26 | |
4a07648a |
27 | my %meth; |
28 | if ($acc_type eq 'single') { |
29 | $meth{$rel} = sub { |
30 | my $self = shift; |
6243d42b |
31 | |
4a07648a |
32 | if (@_) { |
33 | $self->set_from_related($rel, @_); |
34 | return $self->{_relationship_data}{$rel} = $_[0]; |
6243d42b |
35 | } |
36 | elsif (exists $self->{_relationship_data}{$rel}) { |
4a07648a |
37 | return $self->{_relationship_data}{$rel}; |
6243d42b |
38 | } |
39 | else { |
40 | my $rel_info = $class->relationship_info($rel); |
6d0ee587 |
41 | my $cond = $self->result_source->_resolve_condition( |
16053767 |
42 | $rel_info->{cond}, $rel, $self, $rel |
84a1c93f |
43 | ); |
7a7e8718 |
44 | if ($rel_info->{attrs}->{undef_on_null_fk}){ |
e0d8aec0 |
45 | return undef unless ref($cond) eq 'HASH'; |
46 | return undef if grep { not defined $_ } values %$cond; |
7a7e8718 |
47 | } |
1a14aa3f |
48 | my $val = $self->find_related($rel, {}, {}); |
e0d8aec0 |
49 | return $val unless $val; # $val instead of undef so that null-objects can go through |
cde1c1e1 |
50 | |
b28cc0ba |
51 | return $self->{_relationship_data}{$rel} = $val; |
4a07648a |
52 | } |
53 | }; |
6243d42b |
54 | } |
55 | elsif ($acc_type eq 'filter') { |
e705f529 |
56 | $class->throw_exception("No such column '$rel' to filter") |
103647d5 |
57 | unless $class->has_column($rel); |
6243d42b |
58 | |
4685e006 |
59 | my $f_class = $class->relationship_info($rel)->{class}; |
6dd43920 |
60 | |
6243d42b |
61 | $class->inflate_column($rel, { |
62 | inflate => sub { |
63 | my ($val, $self) = @_; |
64 | return $self->find_or_new_related($rel, {}, {}); |
65 | }, |
66 | deflate => sub { |
67 | my ($val, $self) = @_; |
68 | $self->throw_exception("'$val' isn't a $f_class") unless $val->isa($f_class); |
6dd43920 |
69 | |
6243d42b |
70 | # MASSIVE FIXME - this code assumes we pointed at the PK, but the belongs_to |
71 | # helper does not check any of this |
72 | # fixup the code a bit to make things saner, but ideally 'filter' needs to |
73 | # be deprecated ASAP and removed shortly after |
74 | # Not doing so before 0.08250 however, too many things in motion already |
75 | my ($pk_col, @rest) = $val->result_source->_pri_cols_or_die; |
76 | $self->throw_exception( |
77 | "Relationship '$rel' of type 'filter' can not work with a multicolumn primary key on source '$f_class'" |
78 | ) if @rest; |
79 | |
80 | my $pk_val = $val->get_column($pk_col); |
81 | carp_unique ( |
82 | "Unable to deflate 'filter'-type relationship '$rel' (related object " |
83 | . "primary key not retrieved), assuming undef instead" |
84 | ) if ( ! defined $pk_val and $val->in_storage ); |
85 | |
86 | return $pk_val; |
87 | }, |
88 | }); |
89 | } |
90 | elsif ($acc_type eq 'multi') { |
6dd43920 |
91 | |
a9da9b6a |
92 | $meth{$rel} = sub { |
e89c7968 |
93 | DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = fail_on_internal_wantarray; |
a9da9b6a |
94 | shift->search_related($rel, @_) |
95 | }; |
5b89a768 |
96 | $meth{"${rel}_rs"} = sub { shift->search_related_rs($rel, @_) }; |
4a07648a |
97 | $meth{"add_to_${rel}"} = sub { shift->create_related($rel, @_); }; |
6243d42b |
98 | } |
99 | else { |
e705f529 |
100 | $class->throw_exception("No such relationship accessor type '$acc_type'"); |
4a07648a |
101 | } |
6243d42b |
102 | |
4a07648a |
103 | { |
104 | no strict 'refs'; |
105 | no warnings 'redefine'; |
106 | foreach my $meth (keys %meth) { |
ddc0a6c8 |
107 | my $name = join '::', $class, $meth; |
6dd43920 |
108 | *$name = subname($name, $meth{$meth}); |
4a07648a |
109 | } |
110 | } |
111 | } |
112 | |
113 | 1; |