1 package # hide from PAUSE
2 DBIx::Class::Relationship::Accessor;
7 use Class::Inspector ();
9 sub register_relationship {
10 my ($class, $rel, $info) = @_;
11 if (my $acc_type = $info->{attrs}{accessor}) {
12 $class->add_relationship_accessor($rel => $acc_type);
14 $class->next::method($rel => $info);
17 sub add_relationship_accessor {
18 my ($class, $rel, $acc_type) = @_;
20 if ($acc_type eq 'single') {
24 $self->set_from_related($rel, @_);
25 return $self->{_relationship_data}{$rel} = $_[0];
26 } elsif (exists $self->{_relationship_data}{$rel}) {
27 return $self->{_relationship_data}{$rel};
29 my $val = $self->find_related($rel, {}, {});
31 return $self->{_relationship_data}{$rel} = $val;
34 } elsif ($acc_type eq 'filter') {
35 $class->throw_exception("No such column $rel to filter")
36 unless $class->has_column($rel);
37 my $f_class = $class->relationship_info($rel)->{class};
38 $class->inflate_column($rel,
40 my ($val, $self) = @_;
41 return $self->find_or_new_related($rel, {}, {});
44 my ($val, $self) = @_;
45 $self->throw_exception("$val isn't a $f_class") unless $val->isa($f_class);
46 return ($val->_ident_values)[0];
47 # WARNING: probably breaks for multi-pri sometimes. FIXME
51 } elsif ($acc_type eq 'multi') {
52 $meth{$rel} = sub { shift->search_related($rel, @_) };
53 $meth{"${rel}_rs"} = sub { shift->search_related_rs($rel, @_) };
54 $meth{"add_to_${rel}"} = sub { shift->create_related($rel, @_); };
56 $class->throw_exception("No such relationship accessor type $acc_type");
60 no warnings 'redefine';
61 foreach my $meth (keys %meth) {
62 my $name = join '::', $class, $meth;
63 *$name = Sub::Name::subname($name, $meth{$meth});