Make 'filter' rels work half-way sanely with partial prefetch
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / Accessor.pm
CommitLineData
75d07914 1package # hide from PAUSE
c0e7b4e5 2 DBIx::Class::Relationship::Accessor;
4a07648a 3
4use strict;
5use warnings;
6dd43920 6use Sub::Name;
7use DBIx::Class::Carp;
8use namespace::clean;
4a07648a 9
8273e845 10our %_pod_inherit_config =
044e70c7 11 (
12 class_map => { 'DBIx::Class::Relationship::Accessor' => 'DBIx::Class::Relationship' }
13 );
14
71e65b39 15sub register_relationship {
16 my ($class, $rel, $info) = @_;
17 if (my $acc_type = $info->{attrs}{accessor}) {
223b8fe3 18 $class->add_relationship_accessor($rel => $acc_type);
4a07648a 19 }
71e65b39 20 $class->next::method($rel => $info);
4a07648a 21}
22
223b8fe3 23sub add_relationship_accessor {
4a07648a 24 my ($class, $rel, $acc_type) = @_;
25 my %meth;
26 if ($acc_type eq 'single') {
7a7e8718 27 my $rel_info = $class->relationship_info($rel);
4a07648a 28 $meth{$rel} = sub {
29 my $self = shift;
30 if (@_) {
31 $self->set_from_related($rel, @_);
32 return $self->{_relationship_data}{$rel} = $_[0];
33 } elsif (exists $self->{_relationship_data}{$rel}) {
34 return $self->{_relationship_data}{$rel};
35 } else {
6d0ee587 36 my $cond = $self->result_source->_resolve_condition(
16053767 37 $rel_info->{cond}, $rel, $self, $rel
84a1c93f 38 );
7a7e8718 39 if ($rel_info->{attrs}->{undef_on_null_fk}){
e0d8aec0 40 return undef unless ref($cond) eq 'HASH';
41 return undef if grep { not defined $_ } values %$cond;
7a7e8718 42 }
1a14aa3f 43 my $val = $self->find_related($rel, {}, {});
e0d8aec0 44 return $val unless $val; # $val instead of undef so that null-objects can go through
cde1c1e1 45
b28cc0ba 46 return $self->{_relationship_data}{$rel} = $val;
4a07648a 47 }
48 };
49 } elsif ($acc_type eq 'filter') {
e705f529 50 $class->throw_exception("No such column '$rel' to filter")
103647d5 51 unless $class->has_column($rel);
4685e006 52 my $f_class = $class->relationship_info($rel)->{class};
4a07648a 53 $class->inflate_column($rel,
54 { inflate => sub {
55 my ($val, $self) = @_;
e60dc79f 56 return $self->find_or_new_related($rel, {}, {});
4a07648a 57 },
58 deflate => sub {
59 my ($val, $self) = @_;
e705f529 60 $self->throw_exception("'$val' isn't a $f_class") unless $val->isa($f_class);
6dd43920 61
62 # MASSIVE FIXME - this code assumes we pointed at the PK, but the belongs_to
63 # helper does not check any of this
64 # fixup the code a bit to make things saner, but ideally 'filter' needs to
65 # be deprecated ASAP and removed shortly after
66 # Not doing so before 0.08250 however, too many things in motion already
67 my ($pk_col, @rest) = $val->_pri_cols;
68 $self->throw_exception(
69 "Relationship '$rel' of type 'filter' can not work with a multicolumn primary key on source '$f_class'"
70 ) if @rest;
71
72 my $v = $val->$pk_col;
73 carp_unique (
74 "Unable to deflate 'filter'-type relationship '$rel' (related object "
75 . "primary key not retrieved), assuming undef instead"
76 ) if ( ! defined $v and $val->in_storage );
77
78 return $v;
4a07648a 79 }
80 }
81 );
82 } elsif ($acc_type eq 'multi') {
83 $meth{$rel} = sub { shift->search_related($rel, @_) };
5b89a768 84 $meth{"${rel}_rs"} = sub { shift->search_related_rs($rel, @_) };
4a07648a 85 $meth{"add_to_${rel}"} = sub { shift->create_related($rel, @_); };
86 } else {
e705f529 87 $class->throw_exception("No such relationship accessor type '$acc_type'");
4a07648a 88 }
89 {
90 no strict 'refs';
91 no warnings 'redefine';
92 foreach my $meth (keys %meth) {
ddc0a6c8 93 my $name = join '::', $class, $meth;
6dd43920 94 *$name = subname($name, $meth{$meth});
4a07648a 95 }
96 }
97}
98
991;