1 package # hide from PAUSE
2 DBIx::Class::Relationship::Accessor;
10 our %_pod_inherit_config =
12 class_map => { 'DBIx::Class::Relationship::Accessor' => 'DBIx::Class::Relationship' }
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);
20 $class->next::method($rel => $info);
23 sub add_relationship_accessor {
24 my ($class, $rel, $acc_type) = @_;
26 if ($acc_type eq 'single') {
27 my $rel_info = $class->relationship_info($rel);
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};
36 my $cond = $self->result_source->_resolve_condition(
37 $rel_info->{cond}, $rel, $self, $rel
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;
43 my $val = $self->find_related($rel, {}, {});
44 return $val unless $val; # $val instead of undef so that null-objects can go through
46 return $self->{_relationship_data}{$rel} = $val;
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,
55 my ($val, $self) = @_;
56 return $self->find_or_new_related($rel, {}, {});
59 my ($val, $self) = @_;
60 $self->throw_exception("'$val' isn't a $f_class") unless $val->isa($f_class);
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->_pri_cols;
68 $self->throw_exception(
69 "Relationship '$rel' of type 'filter' can not work with a multicolumn primary key on source '$f_class'"
72 my $v = $val->$pk_col;
74 "Unable to deflate 'filter'-type relationship '$rel' (related object "
75 . "primary key not retrieved), assuming undef instead"
76 ) if ( ! defined $v and $val->in_storage );
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, @_); };
87 $class->throw_exception("No such relationship accessor type '$acc_type'");
91 no warnings 'redefine';
92 foreach my $meth (keys %meth) {
93 my $name = join '::', $class, $meth;
94 *$name = subname($name, $meth{$meth});