0dab6409daa72c09c47863f1129b2ff0011d31c2
[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     quote_sub "${class}::${rel}" => sprintf(<<'EOC', perlstring $rel);
28       my $self = shift;
29
30       if (@_) {
31         $self->set_from_related( %1$s => @_ );
32         return $self->{_relationship_data}{%1$s} = $_[0];
33       }
34       elsif (exists $self->{_relationship_data}{%1$s}) {
35         return $self->{_relationship_data}{%1$s};
36       }
37       else {
38         my $rsrc = $self->result_source;
39
40         my $relcond = $rsrc->_resolve_relationship_condition(
41           rel_name => %1$s,
42           foreign_alias => %1$s,
43           self_alias => 'me',
44           self_result_object => $self,
45         );
46
47         return undef if (
48           $relcond->{join_free_condition}
49             and
50           $relcond->{join_free_condition} ne DBIx::Class::_Util::UNRESOLVABLE_CONDITION
51             and
52           scalar grep { not defined $_ } values %%{ $relcond->{join_free_condition} || {} }
53             and
54           $rsrc->relationship_info(%1$s)->{attrs}{undef_on_null_fk}
55         );
56
57         my $val = $self->related_resultset( %1$s )->single;
58         return $val unless $val;  # $val instead of undef so that null-objects can go through
59
60         return $self->{_relationship_data}{%1$s} = $val;
61       }
62 EOC
63   }
64   elsif ($acc_type eq 'filter') {
65     $class->throw_exception("No such column '$rel' to filter")
66        unless $class->result_source_instance->has_column($rel);
67
68     my $f_class = $class->result_source_instance
69                          ->relationship_info($rel)
70                           ->{class};
71
72     $class->inflate_column($rel, {
73       inflate => sub {
74         my ($val, $self) = @_;
75         return $self->find_or_new_related($rel, {});
76       },
77       deflate => sub {
78         my ($val, $self) = @_;
79         $self->throw_exception("'$val' isn't a $f_class") unless $val->isa($f_class);
80
81         # MASSIVE FIXME - this code assumes we pointed at the PK, but the belongs_to
82         # helper does not check any of this
83         # fixup the code a bit to make things saner, but ideally 'filter' needs to
84         # be deprecated ASAP and removed shortly after
85         # Not doing so before 0.08250 however, too many things in motion already
86         my ($pk_col, @rest) = $val->result_source->_pri_cols_or_die;
87         $self->throw_exception(
88           "Relationship '$rel' of type 'filter' can not work with a multicolumn primary key on source '$f_class'"
89         ) if @rest;
90
91         my $pk_val = $val->get_column($pk_col);
92         carp_unique (
93           "Unable to deflate 'filter'-type relationship '$rel' (related object "
94         . "primary key not retrieved), assuming undef instead"
95         ) if ( ! defined $pk_val and $val->in_storage );
96
97         return $pk_val;
98       },
99     });
100   }
101   elsif ($acc_type eq 'multi') {
102
103     quote_sub "${class}::${rel}_rs", "shift->related_resultset( q{$rel} )->search_rs( \@_ )";
104     quote_sub "${class}::add_to_${rel}", "shift->create_related( q{$rel} => \@_ )";
105     quote_sub "${class}::${rel}", sprintf( <<'EOC', perlstring $rel );
106       DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = DBIx::Class::_Util::fail_on_internal_wantarray;
107       shift->related_resultset(%s)->search( @_ )
108 EOC
109   }
110   else {
111     $class->throw_exception("No such relationship accessor type '$acc_type'");
112   }
113
114 }
115
116 1;