d281e00ca62d1bb140f1b6623b08f997154319a9
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / Accessor.pm
1 package # hide from PAUSE
2     DBIx::Class::Relationship::Accessor;
3
4 use strict;
5 use warnings;
6 use DBIx::Class::Carp;
7 use DBIx::Class::_Util qw(quote_sub perlstring);
8 use namespace::clean;
9
10 our %_pod_inherit_config =
11   (
12    class_map => { 'DBIx::Class::Relationship::Accessor' => 'DBIx::Class::Relationship' }
13   );
14
15 sub register_relationship {
16   my ($class, $rel, $info) = @_;
17   if (my $acc_type = $info->{attrs}{accessor}) {
18     $class->add_relationship_accessor($rel => $acc_type);
19   }
20   $class->next::method($rel => $info);
21 }
22
23 sub add_relationship_accessor {
24   my ($class, $rel, $acc_type) = @_;
25
26   if ($acc_type eq 'single') {
27
28     quote_sub "${class}::${rel}" => sprintf(<<'EOC', perlstring $rel);
29
30       my $self = shift;
31
32       if (@_) {
33         $self->set_from_related( %1$s => @_ );
34         return $self->{_relationship_data}{%1$s} = $_[0];
35       }
36       elsif (exists $self->{_relationship_data}{%1$s}) {
37         return $self->{_relationship_data}{%1$s};
38       }
39       else {
40         my $rsrc = $self->result_source;
41
42         my $relcond = $rsrc->_resolve_relationship_condition(
43           rel_name => %1$s,
44           foreign_alias => %1$s,
45           self_alias => 'me',
46           self_result_object => $self,
47         );
48
49         return undef if (
50           $relcond->{join_free_condition}
51             and
52           $relcond->{join_free_condition} ne DBIx::Class::_Util::UNRESOLVABLE_CONDITION
53             and
54           scalar grep { not defined $_ } values %%{ $relcond->{join_free_condition} || {} }
55             and
56           $rsrc->relationship_info(%1$s)->{attrs}{undef_on_null_fk}
57         );
58
59         my $val = $self->related_resultset( %1$s )->single;
60         return $val unless $val;  # $val instead of undef so that null-objects can go through
61
62         return $self->{_relationship_data}{%1$s} = $val;
63       }
64 EOC
65   }
66   elsif ($acc_type eq 'filter') {
67
68     my $rsrc = $class->result_source_instance;
69
70     $rsrc->throw_exception("No such column '$rel' to filter")
71        unless $rsrc->has_column($rel);
72
73     my $f_class = $rsrc->relationship_info($rel)->{class};
74
75     $class->inflate_column($rel, {
76       inflate => sub {
77         my ($val, $self) = @_;
78         return $self->find_or_new_related($rel, {});
79       },
80       deflate => sub {
81         my ($val, $self) = @_;
82         $self->throw_exception("'$val' isn't a $f_class") unless $val->isa($f_class);
83
84         # MASSIVE FIXME - this code assumes we pointed at the PK, but the belongs_to
85         # helper does not check any of this
86         # fixup the code a bit to make things saner, but ideally 'filter' needs to
87         # be deprecated ASAP and removed shortly after
88         # Not doing so before 0.08250 however, too many things in motion already
89         my ($pk_col, @rest) = $val->result_source->_pri_cols_or_die;
90         $self->throw_exception(
91           "Relationship '$rel' of type 'filter' can not work with a multicolumn primary key on source '$f_class'"
92         ) if @rest;
93
94         my $pk_val = $val->get_column($pk_col);
95         carp_unique (
96           "Unable to deflate 'filter'-type relationship '$rel' (related object "
97         . "primary key not retrieved), assuming undef instead"
98         ) if ( ! defined $pk_val and $val->in_storage );
99
100         return $pk_val;
101       },
102     });
103   }
104   elsif ($acc_type eq 'multi') {
105
106
107     quote_sub "${class}::${rel}", sprintf( <<'EOC', perlstring $rel );
108       DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and DBIx::Class::_Util::fail_on_internal_call;
109       DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = DBIx::Class::_Util::fail_on_internal_wantarray;
110       shift->related_resultset(%s)->search( @_ )
111 EOC
112
113
114     quote_sub "${class}::${rel}_rs", sprintf( <<'EOC', perlstring $rel );
115       DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and DBIx::Class::_Util::fail_on_internal_call;
116       shift->related_resultset(%s)->search_rs( @_ )
117 EOC
118
119
120     quote_sub "${class}::add_to_${rel}", sprintf( <<'EOC', perlstring $rel );
121       DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and DBIx::Class::_Util::fail_on_internal_call;
122       shift->create_related( %s => @_ );
123 EOC
124
125   }
126   else {
127     $class->throw_exception("No such relationship accessor type '$acc_type'");
128   }
129
130 }
131
132 1;