c48e80fe01411e480622be530d83f991d8a5e4a6
[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 Sub::Name;
7 use DBIx::Class::Carp;
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   my %meth;
26   if ($acc_type eq 'single') {
27     my $rel_info = $class->relationship_info($rel);
28     $meth{$rel} = sub {
29       my $self = shift;
30       if (@_) {
31         $self->set_from_related($rel, @_);
32         return $self->{_relationship_data}{$rel} = $_[0];
33       } elsif (exists $self->{_relationship_data}{$rel}) {
34         return $self->{_relationship_data}{$rel};
35       } else {
36         my $cond = $self->result_source->_resolve_condition(
37           $rel_info->{cond}, $rel, $self, $rel
38         );
39         if ($rel_info->{attrs}->{undef_on_null_fk}){
40           return undef unless ref($cond) eq 'HASH';
41           return undef if grep { not defined $_ } values %$cond;
42         }
43         my $val = $self->find_related($rel, {}, {});
44         return $val unless $val;  # $val instead of undef so that null-objects can go through
45
46         return $self->{_relationship_data}{$rel} = $val;
47       }
48     };
49   } elsif ($acc_type eq 'filter') {
50     $class->throw_exception("No such column '$rel' to filter")
51        unless $class->has_column($rel);
52     my $f_class = $class->relationship_info($rel)->{class};
53     $class->inflate_column($rel,
54       { inflate => sub {
55           my ($val, $self) = @_;
56           return $self->find_or_new_related($rel, {}, {});
57         },
58         deflate => sub {
59           my ($val, $self) = @_;
60           $self->throw_exception("'$val' isn't a $f_class") unless $val->isa($f_class);
61
62           # MASSIVE FIXME - this code assumes we pointed at the PK, but the belongs_to
63           # helper does not check any of this
64           # fixup the code a bit to make things saner, but ideally 'filter' needs to
65           # be deprecated ASAP and removed shortly after
66           # Not doing so before 0.08250 however, too many things in motion already
67           my ($pk_col, @rest) = $val->result_source->_pri_cols_or_die;
68           $self->throw_exception(
69             "Relationship '$rel' of type 'filter' can not work with a multicolumn primary key on source '$f_class'"
70           ) if @rest;
71
72           my $pk_val = $val->get_column($pk_col);
73           carp_unique (
74             "Unable to deflate 'filter'-type relationship '$rel' (related object "
75           . "primary key not retrieved), assuming undef instead"
76           ) if ( ! defined $pk_val and $val->in_storage );
77
78           return $pk_val;
79         }
80       }
81     );
82   } elsif ($acc_type eq 'multi') {
83     $meth{$rel} = sub { shift->search_related($rel, @_) };
84     $meth{"${rel}_rs"} = sub { shift->search_related_rs($rel, @_) };
85     $meth{"add_to_${rel}"} = sub { shift->create_related($rel, @_); };
86   } else {
87     $class->throw_exception("No such relationship accessor type '$acc_type'");
88   }
89   {
90     no strict 'refs';
91     no warnings 'redefine';
92     foreach my $meth (keys %meth) {
93       my $name = join '::', $class, $meth;
94       *$name = subname($name, $meth{$meth});
95     }
96   }
97 }
98
99 1;