1 package # hide from PAUSE
2 DBIx::Class::Relationship::Accessor;
7 use DBIx::Class::_Util qw(quote_sub perlstring);
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 quote_sub "${class}::${rel}" => sprintf(<<'EOC', perlstring $rel);
31 $self->set_from_related( %1$s => @_ );
32 return $self->{_relationship_data}{%1$s} = $_[0];
34 elsif (exists $self->{_relationship_data}{%1$s}) {
35 return $self->{_relationship_data}{%1$s};
38 my $relcond = $self->result_source->_resolve_relationship_condition(
40 foreign_alias => %1$s,
42 self_result_object => $self,
46 $relcond->{join_free_condition}
48 $relcond->{join_free_condition} ne DBIx::Class::_Util::UNRESOLVABLE_CONDITION
50 scalar grep { not defined $_ } values %%{ $relcond->{join_free_condition} || {} }
52 $self->result_source->relationship_info(%1$s)->{attrs}{undef_on_null_fk}
55 my $val = $self->search_related( %1$s )->single;
56 return $val unless $val; # $val instead of undef so that null-objects can go through
58 return $self->{_relationship_data}{%1$s} = $val;
62 elsif ($acc_type eq 'filter') {
63 $class->throw_exception("No such column '$rel' to filter")
64 unless $class->has_column($rel);
66 my $f_class = $class->relationship_info($rel)->{class};
68 $class->inflate_column($rel, {
70 my ($val, $self) = @_;
71 return $self->find_or_new_related($rel, {}, {});
74 my ($val, $self) = @_;
75 $self->throw_exception("'$val' isn't a $f_class") unless $val->isa($f_class);
77 # MASSIVE FIXME - this code assumes we pointed at the PK, but the belongs_to
78 # helper does not check any of this
79 # fixup the code a bit to make things saner, but ideally 'filter' needs to
80 # be deprecated ASAP and removed shortly after
81 # Not doing so before 0.08250 however, too many things in motion already
82 my ($pk_col, @rest) = $val->result_source->_pri_cols_or_die;
83 $self->throw_exception(
84 "Relationship '$rel' of type 'filter' can not work with a multicolumn primary key on source '$f_class'"
87 my $pk_val = $val->get_column($pk_col);
89 "Unable to deflate 'filter'-type relationship '$rel' (related object "
90 . "primary key not retrieved), assuming undef instead"
91 ) if ( ! defined $pk_val and $val->in_storage );
97 elsif ($acc_type eq 'multi') {
99 quote_sub "${class}::${rel}_rs", "shift->search_related_rs( $rel => \@_ )";
100 quote_sub "${class}::add_to_${rel}", "shift->create_related( $rel => \@_ )";
101 quote_sub "${class}::${rel}", sprintf( <<'EOC', perlstring $rel );
102 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = DBIx::Class::_Util::fail_on_internal_wantarray;
103 shift->search_related( %s => @_ )
107 $class->throw_exception("No such relationship accessor type '$acc_type'");