Reduce duplicate ->result_source calls where sensible
[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;
6dd43920 6use DBIx::Class::Carp;
8d73fcd4 7use DBIx::Class::_Util qw(quote_sub perlstring);
6dd43920 8use namespace::clean;
4a07648a 9
8273e845 10our %_pod_inherit_config =
044e70c7 11 (
12 class_map => { 'DBIx::Class::Relationship::Accessor' => 'DBIx::Class::Relationship' }
13 );
14
71e65b39 15sub 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 23sub add_relationship_accessor {
4a07648a 24 my ($class, $rel, $acc_type) = @_;
6243d42b 25
4a07648a 26 if ($acc_type eq 'single') {
8d73fcd4 27 quote_sub "${class}::${rel}" => sprintf(<<'EOC', perlstring $rel);
4a07648a 28 my $self = shift;
6243d42b 29
4a07648a 30 if (@_) {
8d73fcd4 31 $self->set_from_related( %1$s => @_ );
32 return $self->{_relationship_data}{%1$s} = $_[0];
6243d42b 33 }
8d73fcd4 34 elsif (exists $self->{_relationship_data}{%1$s}) {
35 return $self->{_relationship_data}{%1$s};
6243d42b 36 }
37 else {
5567c8f8 38 my $rsrc = $self->result_source;
39
40 my $relcond = $rsrc->_resolve_relationship_condition(
139e7991 41 rel_name => %1$s,
42 foreign_alias => %1$s,
43 self_alias => 'me',
44 self_result_object => $self,
84a1c93f 45 );
139e7991 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
5567c8f8 54 $rsrc->relationship_info(%1$s)->{attrs}{undef_on_null_fk}
139e7991 55 );
56
57 my $val = $self->search_related( %1$s )->single;
e0d8aec0 58 return $val unless $val; # $val instead of undef so that null-objects can go through
cde1c1e1 59
8d73fcd4 60 return $self->{_relationship_data}{%1$s} = $val;
4a07648a 61 }
8d73fcd4 62EOC
6243d42b 63 }
64 elsif ($acc_type eq 'filter') {
e705f529 65 $class->throw_exception("No such column '$rel' to filter")
103647d5 66 unless $class->has_column($rel);
6243d42b 67
4685e006 68 my $f_class = $class->relationship_info($rel)->{class};
6dd43920 69
6243d42b 70 $class->inflate_column($rel, {
71 inflate => sub {
72 my ($val, $self) = @_;
73 return $self->find_or_new_related($rel, {}, {});
74 },
75 deflate => sub {
76 my ($val, $self) = @_;
77 $self->throw_exception("'$val' isn't a $f_class") unless $val->isa($f_class);
6dd43920 78
6243d42b 79 # MASSIVE FIXME - this code assumes we pointed at the PK, but the belongs_to
80 # helper does not check any of this
81 # fixup the code a bit to make things saner, but ideally 'filter' needs to
82 # be deprecated ASAP and removed shortly after
83 # Not doing so before 0.08250 however, too many things in motion already
84 my ($pk_col, @rest) = $val->result_source->_pri_cols_or_die;
85 $self->throw_exception(
86 "Relationship '$rel' of type 'filter' can not work with a multicolumn primary key on source '$f_class'"
87 ) if @rest;
88
89 my $pk_val = $val->get_column($pk_col);
90 carp_unique (
91 "Unable to deflate 'filter'-type relationship '$rel' (related object "
92 . "primary key not retrieved), assuming undef instead"
93 ) if ( ! defined $pk_val and $val->in_storage );
94
95 return $pk_val;
96 },
97 });
98 }
99 elsif ($acc_type eq 'multi') {
6dd43920 100
8d73fcd4 101 quote_sub "${class}::${rel}_rs", "shift->search_related_rs( $rel => \@_ )";
102 quote_sub "${class}::add_to_${rel}", "shift->create_related( $rel => \@_ )";
103 quote_sub "${class}::${rel}", sprintf( <<'EOC', perlstring $rel );
104 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = DBIx::Class::_Util::fail_on_internal_wantarray;
105 shift->search_related( %s => @_ )
106EOC
6243d42b 107 }
108 else {
e705f529 109 $class->throw_exception("No such relationship accessor type '$acc_type'");
4a07648a 110 }
6243d42b 111
4a07648a 112}
113
1141;