1 package DBIx::Class::Relationship::Accessor;
7 my ($class, $rel, @rest) = @_;
8 my $ret = $class->NEXT::ACTUAL::add_relationship($rel => @rest);
9 my $rel_obj = $class->_relationships->{$rel};
10 if (my $acc_type = $rel_obj->{attrs}{accessor}) {
11 $class->add_relationship_accessor($rel => $acc_type);
16 sub add_relationship_accessor {
17 my ($class, $rel, $acc_type) = @_;
19 if ($acc_type eq 'single') {
23 $self->set_from_related($rel, @_);
24 return $self->{_relationship_data}{$rel} = $_[0];
25 } elsif (exists $self->{_relationship_data}{$rel}) {
26 return $self->{_relationship_data}{$rel};
28 my $val = $self->find_related($rel, {}, {});
30 return $self->{_relationship_data}{$rel} = $val;
33 } elsif ($acc_type eq 'filter') {
34 $class->throw("No such column $rel to filter")
35 unless exists $class->_columns->{$rel};
36 my $f_class = $class->_relationships->{$rel}{class};
37 $class->inflate_column($rel,
39 my ($val, $self) = @_;
40 return $self->find_or_create_related($rel, {}, {});
43 my ($val, $self) = @_;
44 $self->throw("$val isn't a $f_class") unless $val->isa($f_class);
45 return ($val->_ident_values)[0];
46 # WARNING: probably breaks for multi-pri sometimes. FIXME
50 } elsif ($acc_type eq 'multi') {
51 $meth{$rel} = sub { shift->search_related($rel, @_) };
52 $meth{"add_to_${rel}"} = sub { shift->create_related($rel, @_); };
54 $class->throw("No such relationship accessor type $acc_type");
58 no warnings 'redefine';
59 foreach my $meth (keys %meth) {
60 *{"${class}::${meth}"} = $meth{$meth};