1 package # hide from PAUSE
2 DBIx::Class::Relationship::Accessor;
8 use DBIx::Class::_Util 'fail_on_internal_wantarray';
11 our %_pod_inherit_config =
13 class_map => { 'DBIx::Class::Relationship::Accessor' => 'DBIx::Class::Relationship' }
16 sub register_relationship {
17 my ($class, $rel, $info) = @_;
18 if (my $acc_type = $info->{attrs}{accessor}) {
19 $class->add_relationship_accessor($rel => $acc_type);
21 $class->next::method($rel => $info);
24 sub add_relationship_accessor {
25 my ($class, $rel, $acc_type) = @_;
28 if ($acc_type eq 'single') {
33 $self->set_from_related($rel, @_);
34 return $self->{_relationship_data}{$rel} = $_[0];
36 elsif (exists $self->{_relationship_data}{$rel}) {
37 return $self->{_relationship_data}{$rel};
40 my $rel_info = $class->relationship_info($rel);
41 my $cond = $self->result_source->_resolve_condition(
42 $rel_info->{cond}, $rel, $self, $rel
44 if ($rel_info->{attrs}->{undef_on_null_fk}){
45 return undef unless ref($cond) eq 'HASH';
46 return undef if grep { not defined $_ } values %$cond;
48 my $val = $self->find_related($rel, {}, {});
49 return $val unless $val; # $val instead of undef so that null-objects can go through
51 return $self->{_relationship_data}{$rel} = $val;
55 elsif ($acc_type eq 'filter') {
56 $class->throw_exception("No such column '$rel' to filter")
57 unless $class->has_column($rel);
59 my $f_class = $class->relationship_info($rel)->{class};
61 $class->inflate_column($rel, {
63 my ($val, $self) = @_;
64 return $self->find_or_new_related($rel, {}, {});
67 my ($val, $self) = @_;
68 $self->throw_exception("'$val' isn't a $f_class") unless $val->isa($f_class);
70 # MASSIVE FIXME - this code assumes we pointed at the PK, but the belongs_to
71 # helper does not check any of this
72 # fixup the code a bit to make things saner, but ideally 'filter' needs to
73 # be deprecated ASAP and removed shortly after
74 # Not doing so before 0.08250 however, too many things in motion already
75 my ($pk_col, @rest) = $val->result_source->_pri_cols_or_die;
76 $self->throw_exception(
77 "Relationship '$rel' of type 'filter' can not work with a multicolumn primary key on source '$f_class'"
80 my $pk_val = $val->get_column($pk_col);
82 "Unable to deflate 'filter'-type relationship '$rel' (related object "
83 . "primary key not retrieved), assuming undef instead"
84 ) if ( ! defined $pk_val and $val->in_storage );
90 elsif ($acc_type eq 'multi') {
93 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = fail_on_internal_wantarray;
94 shift->search_related($rel, @_)
96 $meth{"${rel}_rs"} = sub { shift->search_related_rs($rel, @_) };
97 $meth{"add_to_${rel}"} = sub { shift->create_related($rel, @_); };
100 $class->throw_exception("No such relationship accessor type '$acc_type'");
105 no warnings 'redefine';
106 foreach my $meth (keys %meth) {
107 my $name = join '::', $class, $meth;
108 *$name = subname($name, $meth{$meth});