introduce row caching using related_resultset; has_many prefetch (single-level only...
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / Relationship / Base.pm
CommitLineData
55e2d745 1package DBIx::Class::Relationship::Base;
2
3use strict;
4use warnings;
5
1edd1722 6use base qw/DBIx::Class/;
55e2d745 7
8__PACKAGE__->mk_classdata('_relationships', { } );
9
10=head1 NAME
11
8918977e 12DBIx::Class::Relationship::Base - Inter-table relationships
55e2d745 13
14=head1 SYNOPSIS
15
16=head1 DESCRIPTION
17
18This class handles relationships between the tables in your database
19model. It allows your to set up relationships, and to perform joins
20on searches.
21
22=head1 METHODS
23
8091aa91 24=head2 add_relationship
503536d5 25
26 __PACKAGE__->add_relationship('relname', 'Foreign::Class', $cond, $attrs);
27
28The condition needs to be an SQL::Abstract-style representation of the
8091aa91 29join between the tables. For example, if you're creating a rel from Foo to Bar,
503536d5 30
31 { 'foreign.foo_id' => 'self.id' }
32
8091aa91 33will result in the JOIN clause
503536d5 34
35 foo me JOIN bar bar ON bar.foo_id = me.id
36
8091aa91 37You can specify as many foreign => self mappings as necessary.
38
39Valid attributes are as follows:
40
41=over 4
42
43=item join_type
44
45Explicitly specifies the type of join to use in the relationship. Any SQL
46join type is valid, e.g. C<LEFT> or C<RIGHT>. It will be placed in the SQL
47command immediately before C<JOIN>.
48
49=item proxy
50
51An arrayref containing a list of accessors in the foreign class to proxy in
52the main class. If, for example, you do the following:
53
fc69fea6 54 __PACKAGE__->might_have(bar => 'Bar', undef, { proxy => [ qw/margle/ ] });
8091aa91 55
56Then, assuming Bar has an accessor named margle, you can do:
57
58 my $obj = Foo->find(1);
59 $obj->margle(10); # set margle; Bar object is created if it doesn't exist
60
61=item accessor
62
63Specifies the type of accessor that should be created for the relationship.
64Valid values are C<single> (for when there is only a single related object),
65C<multi> (when there can be many), and C<filter> (for when there is a single
66related object, but you also want the relationship accessor to double as
67a column accessor). For C<multi> accessors, an add_to_* method is also
68created, which calls C<create_related> for the relationship.
69
70=back
71
71e65b39 72=head2 register_relationship($relname, $rel_info)
73
74Registers a relationship on the class
75
55e2d745 76=cut
77
71e65b39 78sub register_relationship { }
79
8091aa91 80=head2 search_related
503536d5 81
82 My::Table->search_related('relname', $cond, $attrs);
83
84=cut
85
55e2d745 86sub search_related {
87 my $self = shift;
55e2d745 88 my $rel = shift;
64acc2bc 89 my $rs = $self->related_resultset($rel);
90 if( @_ ) {
91 return $rs->search(@_);
55e2d745 92 }
64acc2bc 93 else {
94 # search() returns a new resultset, so related_resultsets would be lost
95 return wantarray ? $rs->all : $rs;
8fab5eef 96 }
b52e9bf8 97}
98
99=head2 count_related
100
7be93b07 101 $obj->count_related('relname', $cond, $attrs);
b52e9bf8 102
103=cut
104
105sub count_related {
106 my $self = shift;
107 return $self->search_related(@_)->count;
55e2d745 108}
109
8091aa91 110=head2 create_related
503536d5 111
112 My::Table->create_related('relname', \%col_data);
113
114=cut
115
55e2d745 116sub create_related {
3842b955 117 my $self = shift;
fea3d045 118 my $rel = shift;
64acc2bc 119 my $obj = $self->search_related($rel)->create(@_);
120 delete $self->{related_resultsets}->{$rel};
121 return $obj;
55e2d745 122}
123
8091aa91 124=head2 new_related
503536d5 125
126 My::Table->new_related('relname', \%col_data);
127
128=cut
129
55e2d745 130sub new_related {
131 my ($self, $rel, $values, $attrs) = @_;
fea3d045 132 return $self->search_related($rel)->new($values, $attrs);
55e2d745 133}
134
8091aa91 135=head2 find_related
503536d5 136
137 My::Table->find_related('relname', @pri_vals | \%pri_vals);
138
139=cut
140
1a14aa3f 141sub find_related {
142 my $self = shift;
143 my $rel = shift;
716b3d29 144 return $self->search_related($rel)->find(@_);
1a14aa3f 145}
146
8091aa91 147=head2 find_or_create_related
503536d5 148
149 My::Table->find_or_create_related('relname', \%col_data);
150
151=cut
152
55e2d745 153sub find_or_create_related {
154 my $self = shift;
1a14aa3f 155 return $self->find_related(@_) || $self->create_related(@_);
55e2d745 156}
157
8091aa91 158=head2 set_from_related
503536d5 159
160 My::Table->set_from_related('relname', $rel_obj);
161
162=cut
163
55e2d745 164sub set_from_related {
165 my ($self, $rel, $f_obj) = @_;
4685e006 166 my $rel_obj = $self->relationship_info($rel);
701da8c4 167 $self->throw_exception( "No such relationship ${rel}" ) unless $rel_obj;
55e2d745 168 my $cond = $rel_obj->{cond};
701da8c4 169 $self->throw_exception( "set_from_related can only handle a hash condition; the "
55e2d745 170 ."condition for $rel is of type ".(ref $cond ? ref $cond : 'plain scalar'))
171 unless ref $cond eq 'HASH';
7fb16f1a 172 my $f_class = $self->result_source->schema->class($rel_obj->{class});
701da8c4 173 $self->throw_exception( "Object $f_obj isn't a ".$f_class )
55e2d745 174 unless $f_obj->isa($f_class);
fde6e28e 175 $self->set_columns(
176 $self->result_source->resolve_condition(
177 $rel_obj->{cond}, $f_obj, $rel));
55e2d745 178 return 1;
179}
180
8091aa91 181=head2 update_from_related
503536d5 182
183 My::Table->update_from_related('relname', $rel_obj);
184
185=cut
186
55e2d745 187sub update_from_related {
188 my $self = shift;
189 $self->set_from_related(@_);
190 $self->update;
191}
192
8091aa91 193=head2 delete_related
503536d5 194
195 My::Table->delete_related('relname', $cond, $attrs);
196
197=cut
198
55e2d745 199sub delete_related {
200 my $self = shift;
64acc2bc 201 my $obj = $self->search_related(@_)->delete;
202 delete $self->{related_resultsets}->{$_[0]};
203 return $obj;
55e2d745 204}
205
2061;
207
64acc2bc 208=head2 related_resultset($name)
209
210Returns a L<DBIx::Class::ResultSet> for the relationship named $name.
211
212 $rs = My::Table->related_resultset('related_table');
213
214=cut
215
216sub related_resultset {
217 my $self = shift;
218 $self->throw_exception("Can't call *_related as class methods") unless ref $self;
219 my $rel = shift;
220 $self->{related_resultsets} ||= {};
221 #use Data::Dumper; warn "related_resultsets: ", Dumper $self->{related_resultsets};
222 my $resultsets = $self->{related_resultsets};
223 if( !exists $resultsets->{$rel} ) {
224
225 #warn "creating related resultset for relation '$rel'", \$self;
226 my $source = $self->result_source;
227 # if relation exists but resultset doesn't, create the resultset
228
229 my $attrs = { };
230 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
231 $attrs = { %{ pop(@_) } };
232 }
233
234 my $rel_obj = $self->relationship_info($rel);
235 $self->throw_exception( "No such relationship ${rel}" ) unless $rel_obj;
236 $attrs = { %{$rel_obj->{attrs} || {}}, %{$attrs || {}} };
237
238 $self->throw_exception( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1));
239 my $query = ((@_ > 1) ? {@_} : shift);
240
241 my ($cond) = $self->result_source->resolve_condition($rel_obj->{cond}, $rel, $self);
242 if (ref $cond eq 'ARRAY') {
243 $cond = [ map { my %hash;
244 foreach my $key (keys %{$_}) {
245 unless ($key =~ m/\./) {
246 $hash{"me.$key"} = $_->{$key};
247 } else {
248 $hash{$key} = $_->{$key};
249 }
250 }; \%hash; } @$cond ];
251 } else {
252 foreach my $key (keys %$cond) {
253 unless ($key =~ m/\./) {
254 $cond->{"me.$key"} = delete $cond->{$key};
255 }
256 }
257 }
258 $query = ($query ? { '-and' => [ $cond, $query ] } : $cond);
259 #use Data::Dumper; warn Dumper($cond);
260 #warn $rel_obj->{class}." $meth $cond ".join(', ', @{$attrs->{bind}||[]});
261 $resultsets->{$rel} =
262 $self->result_source->related_source($rel)->resultset->search($query, $attrs);
263 }
264 return $resultsets->{$rel};
265}
266
55e2d745 267=head1 AUTHORS
268
daec44b8 269Matt S. Trout <mst@shadowcatsystems.co.uk>
55e2d745 270
271=head1 LICENSE
272
273You may distribute this code under the same terms as Perl itself.
274
275=cut
276