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') {
28 quote_sub "${class}::${rel}" => sprintf(<<'EOC', perlstring $rel);
33 $self->set_from_related( %1$s => @_ );
34 return $self->{_relationship_data}{%1$s} = $_[0];
36 elsif (exists $self->{_relationship_data}{%1$s}) {
37 return $self->{_relationship_data}{%1$s};
40 my $rsrc = $self->result_source;
42 my $relcond = $rsrc->_resolve_relationship_condition(
44 foreign_alias => %1$s,
46 self_result_object => $self,
50 $relcond->{join_free_condition}
52 $relcond->{join_free_condition} ne DBIx::Class::_Util::UNRESOLVABLE_CONDITION
54 scalar grep { not defined $_ } values %%{ $relcond->{join_free_condition} || {} }
56 $rsrc->relationship_info(%1$s)->{attrs}{undef_on_null_fk}
59 my $val = $self->related_resultset( %1$s )->single;
60 return $val unless $val; # $val instead of undef so that null-objects can go through
62 return $self->{_relationship_data}{%1$s} = $val;
66 elsif ($acc_type eq 'filter') {
68 my $rsrc = $class->result_source_instance;
70 $rsrc->throw_exception("No such column '$rel' to filter")
71 unless $rsrc->has_column($rel);
73 my $f_class = $rsrc->relationship_info($rel)->{class};
75 $class->inflate_column($rel, {
77 my ($val, $self) = @_;
78 return $self->find_or_new_related($rel, {});
81 my ($val, $self) = @_;
82 $self->throw_exception("'$val' isn't a $f_class") unless $val->isa($f_class);
84 # MASSIVE FIXME - this code assumes we pointed at the PK, but the belongs_to
85 # helper does not check any of this
86 # fixup the code a bit to make things saner, but ideally 'filter' needs to
87 # be deprecated ASAP and removed shortly after
88 # Not doing so before 0.08250 however, too many things in motion already
89 my ($pk_col, @rest) = $val->result_source->_pri_cols_or_die;
90 $self->throw_exception(
91 "Relationship '$rel' of type 'filter' can not work with a multicolumn primary key on source '$f_class'"
94 my $pk_val = $val->get_column($pk_col);
96 "Unable to deflate 'filter'-type relationship '$rel' (related object "
97 . "primary key not retrieved), assuming undef instead"
98 ) if ( ! defined $pk_val and $val->in_storage );
104 elsif ($acc_type eq 'multi') {
107 quote_sub "${class}::${rel}", sprintf( <<'EOC', perlstring $rel );
108 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and DBIx::Class::_Util::fail_on_internal_call;
109 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = DBIx::Class::_Util::fail_on_internal_wantarray;
110 shift->related_resultset(%s)->search( @_ )
114 quote_sub "${class}::${rel}_rs", sprintf( <<'EOC', perlstring $rel );
115 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and DBIx::Class::_Util::fail_on_internal_call;
116 shift->related_resultset(%s)->search_rs( @_ )
120 quote_sub "${class}::add_to_${rel}", sprintf( <<'EOC', perlstring $rel );
121 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and DBIx::Class::_Util::fail_on_internal_call;
122 shift->create_related( %s => @_ );
127 $class->throw_exception("No such relationship accessor type '$acc_type'");