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 DBIx::Class::Carp; |
8d73fcd4 |
7 | use DBIx::Class::_Util qw(quote_sub perlstring); |
6dd43920 |
8 | use namespace::clean; |
4a07648a |
9 | |
8273e845 |
10 | our %_pod_inherit_config = |
044e70c7 |
11 | ( |
12 | class_map => { 'DBIx::Class::Relationship::Accessor' => 'DBIx::Class::Relationship' } |
13 | ); |
14 | |
71e65b39 |
15 | sub register_relationship { |
16 | my ($class, $rel, $info) = @_; |
17 | if (my $acc_type = $info->{attrs}{accessor}) { |
223b8fe3 |
18 | $class->add_relationship_accessor($rel => $acc_type); |
4a07648a |
19 | } |
71e65b39 |
20 | $class->next::method($rel => $info); |
4a07648a |
21 | } |
22 | |
223b8fe3 |
23 | sub add_relationship_accessor { |
4a07648a |
24 | my ($class, $rel, $acc_type) = @_; |
6243d42b |
25 | |
4a07648a |
26 | if ($acc_type eq 'single') { |
d46eac43 |
27 | |
09d8fb4a |
28 | my @qsub_args = ( {}, { |
29 | attributes => [qw( |
30 | DBIC_method_is_single_relationship_accessor |
31 | DBIC_method_is_generated_from_resultsource_metadata |
32 | )] |
33 | }); |
34 | |
d46eac43 |
35 | |
09d8fb4a |
36 | quote_sub "${class}::${rel}" => sprintf(<<'EOC', perlstring $rel), @qsub_args; |
4a07648a |
37 | my $self = shift; |
6243d42b |
38 | |
4a07648a |
39 | if (@_) { |
8d73fcd4 |
40 | $self->set_from_related( %1$s => @_ ); |
41 | return $self->{_relationship_data}{%1$s} = $_[0]; |
6243d42b |
42 | } |
8d73fcd4 |
43 | elsif (exists $self->{_relationship_data}{%1$s}) { |
44 | return $self->{_relationship_data}{%1$s}; |
6243d42b |
45 | } |
46 | else { |
5567c8f8 |
47 | my $rsrc = $self->result_source; |
48 | |
49 | my $relcond = $rsrc->_resolve_relationship_condition( |
139e7991 |
50 | rel_name => %1$s, |
51 | foreign_alias => %1$s, |
52 | self_alias => 'me', |
53 | self_result_object => $self, |
84a1c93f |
54 | ); |
139e7991 |
55 | |
56 | return undef if ( |
57 | $relcond->{join_free_condition} |
58 | and |
59 | $relcond->{join_free_condition} ne DBIx::Class::_Util::UNRESOLVABLE_CONDITION |
60 | and |
61 | scalar grep { not defined $_ } values %%{ $relcond->{join_free_condition} || {} } |
62 | and |
5567c8f8 |
63 | $rsrc->relationship_info(%1$s)->{attrs}{undef_on_null_fk} |
139e7991 |
64 | ); |
65 | |
e5053694 |
66 | my $val = $self->related_resultset( %1$s )->single; |
e0d8aec0 |
67 | return $val unless $val; # $val instead of undef so that null-objects can go through |
cde1c1e1 |
68 | |
8d73fcd4 |
69 | return $self->{_relationship_data}{%1$s} = $val; |
4a07648a |
70 | } |
8d73fcd4 |
71 | EOC |
6243d42b |
72 | } |
73 | elsif ($acc_type eq 'filter') { |
6243d42b |
74 | |
d46eac43 |
75 | my $rsrc = $class->result_source_instance; |
76 | |
77 | $rsrc->throw_exception("No such column '$rel' to filter") |
78 | unless $rsrc->has_column($rel); |
79 | |
80 | my $f_class = $rsrc->relationship_info($rel)->{class}; |
6dd43920 |
81 | |
6243d42b |
82 | $class->inflate_column($rel, { |
83 | inflate => sub { |
84 | my ($val, $self) = @_; |
e5053694 |
85 | return $self->find_or_new_related($rel, {}); |
6243d42b |
86 | }, |
87 | deflate => sub { |
88 | my ($val, $self) = @_; |
89 | $self->throw_exception("'$val' isn't a $f_class") unless $val->isa($f_class); |
6dd43920 |
90 | |
6243d42b |
91 | # MASSIVE FIXME - this code assumes we pointed at the PK, but the belongs_to |
92 | # helper does not check any of this |
93 | # fixup the code a bit to make things saner, but ideally 'filter' needs to |
94 | # be deprecated ASAP and removed shortly after |
95 | # Not doing so before 0.08250 however, too many things in motion already |
96 | my ($pk_col, @rest) = $val->result_source->_pri_cols_or_die; |
97 | $self->throw_exception( |
98 | "Relationship '$rel' of type 'filter' can not work with a multicolumn primary key on source '$f_class'" |
99 | ) if @rest; |
100 | |
101 | my $pk_val = $val->get_column($pk_col); |
102 | carp_unique ( |
103 | "Unable to deflate 'filter'-type relationship '$rel' (related object " |
104 | . "primary key not retrieved), assuming undef instead" |
105 | ) if ( ! defined $pk_val and $val->in_storage ); |
106 | |
107 | return $pk_val; |
108 | }, |
109 | }); |
09d8fb4a |
110 | |
111 | |
112 | # god this is horrible... |
113 | my $acc = |
114 | $rsrc->columns_info->{$rel}{accessor} |
115 | || |
116 | $rel |
117 | ; |
118 | |
119 | # because CDBI may elect to never make an accessor at all... |
120 | if( my $main_cref = $class->can($acc) ) { |
121 | |
122 | attributes->import( |
123 | $class, |
124 | $main_cref, |
125 | qw( |
126 | DBIC_method_is_filter_relationship_accessor |
127 | DBIC_method_is_generated_from_resultsource_metadata |
128 | ), |
129 | ); |
130 | |
131 | if( my $extra_cref = $class->can("_${acc}_accessor") ) { |
132 | attributes->import( |
133 | $class, |
134 | $extra_cref, |
135 | qw( |
136 | DBIC_method_is_filter_relationship_extra_accessor |
137 | DBIC_method_is_generated_from_resultsource_metadata |
138 | ), |
139 | ); |
140 | } |
141 | } |
6243d42b |
142 | } |
143 | elsif ($acc_type eq 'multi') { |
6dd43920 |
144 | |
d46eac43 |
145 | |
1b822bd3 |
146 | my @qsub_args = ( |
147 | {}, |
148 | { |
149 | attributes => [qw( |
09d8fb4a |
150 | DBIC_method_is_multi_relationship_accessor |
151 | DBIC_method_is_generated_from_resultsource_metadata |
1b822bd3 |
152 | DBIC_method_is_indirect_sugar |
153 | )] |
154 | }, |
155 | ); |
156 | |
157 | |
158 | quote_sub "${class}::${rel}", sprintf( <<'EOC', perlstring $rel ), @qsub_args; |
d46eac43 |
159 | DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and DBIx::Class::_Util::fail_on_internal_call; |
160 | DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = DBIx::Class::_Util::fail_on_internal_wantarray; |
161 | shift->related_resultset(%s)->search( @_ ) |
162 | EOC |
163 | |
164 | |
09d8fb4a |
165 | $qsub_args[1]{attributes}[0] |
166 | =~ s/^DBIC_method_is_multi_relationship_accessor$/DBIC_method_is_multi_relationship_extra_accessor/ |
167 | or die "Unexpected attr '$qsub_args[1]{attributes}[0]' ..."; |
168 | |
169 | |
1b822bd3 |
170 | quote_sub "${class}::${rel}_rs", sprintf( <<'EOC', perlstring $rel ), @qsub_args; |
d99f2db7 |
171 | DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and DBIx::Class::_Util::fail_on_internal_call; |
172 | shift->related_resultset(%s)->search_rs( @_ ) |
173 | EOC |
174 | |
d46eac43 |
175 | |
1b822bd3 |
176 | quote_sub "${class}::add_to_${rel}", sprintf( <<'EOC', perlstring $rel ), @qsub_args; |
d99f2db7 |
177 | DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and DBIx::Class::_Util::fail_on_internal_call; |
178 | shift->create_related( %s => @_ ); |
179 | EOC |
180 | |
6243d42b |
181 | } |
182 | else { |
e705f529 |
183 | $class->throw_exception("No such relationship accessor type '$acc_type'"); |
4a07648a |
184 | } |
6243d42b |
185 | |
4a07648a |
186 | } |
187 | |
188 | 1; |