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