1 package # hide from PAUSE
2 DBIx::Class::Relationship::Accessor;
7 sub register_relationship {
8 my ($class, $rel, $info) = @_;
9 if (my $acc_type = $info->{attrs}{accessor}) {
10 $class->add_relationship_accessor($rel => $acc_type);
12 $class->next::method($rel => $info);
15 sub add_relationship_accessor {
16 my ($class, $rel, $acc_type) = @_;
18 if ($acc_type eq 'single') {
22 $self->set_from_related($rel, @_);
23 return $self->{_relationship_data}{$rel} = $_[0];
24 } elsif (exists $self->{_relationship_data}{$rel}) {
25 return $self->{_relationship_data}{$rel};
27 my $val = $self->find_related($rel, {}, {});
29 return $self->{_relationship_data}{$rel} = $val;
32 } elsif ($acc_type eq 'filter') {
33 $class->throw_exception("No such column $rel to filter")
34 unless $class->has_column($rel);
35 my $f_class = $class->relationship_info($rel)->{class};
36 $class->inflate_column($rel,
38 my ($val, $self) = @_;
39 return $self->find_or_create_related($rel, {}, {});
42 my ($val, $self) = @_;
43 $self->throw_exception("$val isn't a $f_class") unless $val->isa($f_class);
44 return ($val->_ident_values)[0];
45 # WARNING: probably breaks for multi-pri sometimes. FIXME
49 } elsif ($acc_type eq 'multi') {
50 $meth{$rel} = sub { shift->search_related($rel, @_) };
51 $meth{"add_to_${rel}"} = sub { shift->create_related($rel, @_); };
53 $class->throw_exception("No such relationship accessor type $acc_type");
57 no warnings 'redefine';
58 foreach my $meth (keys %meth) {
59 *{"${class}::${meth}"} = $meth{$meth};