pod =head3 Arguments stuff
[dbsrgits/DBIx-Class.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
87c4e602 72=head2 register_relationship
73
74=head3 Arguments: ($relname, $rel_info)
71e65b39 75
76Registers a relationship on the class
77
55e2d745 78=cut
79
71e65b39 80sub register_relationship { }
81
8091aa91 82=head2 search_related
503536d5 83
84 My::Table->search_related('relname', $cond, $attrs);
85
86=cut
87
55e2d745 88sub search_related {
89 my $self = shift;
3842b955 90 die "Can't call *_related as class methods" unless ref $self;
55e2d745 91 my $rel = shift;
92 my $attrs = { };
93 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
94 $attrs = { %{ pop(@_) } };
95 }
4685e006 96 my $rel_obj = $self->relationship_info($rel);
701da8c4 97 $self->throw_exception( "No such relationship ${rel}" ) unless $rel_obj;
55e2d745 98 $attrs = { %{$rel_obj->{attrs} || {}}, %{$attrs || {}} };
99
701da8c4 100 $self->throw_exception( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1));
55e2d745 101 my $query = ((@_ > 1) ? {@_} : shift);
102
3842b955 103 my ($cond) = $self->result_source->resolve_condition($rel_obj->{cond}, $rel, $self);
ca4b5ab7 104 if (ref $cond eq 'ARRAY') {
105 $cond = [ map { my %hash;
106 foreach my $key (keys %{$_}) {
107 unless ($key =~ m/\./) {
108 $hash{"me.$key"} = $_->{$key};
109 } else {
110 $hash{$key} = $_->{$key};
111 }
112 }; \%hash; } @$cond ];
113 } else {
114 foreach my $key (keys %$cond) {
115 unless ($key =~ m/\./) {
116 $cond->{"me.$key"} = delete $cond->{$key};
117 }
8fab5eef 118 }
119 }
438adc0e 120 $query = ($query ? { '-and' => [ $cond, $query ] } : $cond);
3842b955 121 #use Data::Dumper; warn Dumper($cond);
438adc0e 122 #warn $rel_obj->{class}." $meth $cond ".join(', ', @{$attrs->{bind}||[]});
3842b955 123 return $self->result_source->related_source($rel
124 )->resultset->search($query, $attrs);
b52e9bf8 125}
126
127=head2 count_related
128
7be93b07 129 $obj->count_related('relname', $cond, $attrs);
b52e9bf8 130
131=cut
132
133sub count_related {
134 my $self = shift;
135 return $self->search_related(@_)->count;
55e2d745 136}
137
8091aa91 138=head2 create_related
503536d5 139
140 My::Table->create_related('relname', \%col_data);
141
142=cut
143
55e2d745 144sub create_related {
3842b955 145 my $self = shift;
fea3d045 146 my $rel = shift;
3842b955 147 return $self->search_related($rel)->create(@_);
55e2d745 148}
149
8091aa91 150=head2 new_related
503536d5 151
152 My::Table->new_related('relname', \%col_data);
153
154=cut
155
55e2d745 156sub new_related {
157 my ($self, $rel, $values, $attrs) = @_;
fea3d045 158 return $self->search_related($rel)->new($values, $attrs);
55e2d745 159}
160
8091aa91 161=head2 find_related
503536d5 162
163 My::Table->find_related('relname', @pri_vals | \%pri_vals);
164
165=cut
166
1a14aa3f 167sub find_related {
168 my $self = shift;
169 my $rel = shift;
716b3d29 170 return $self->search_related($rel)->find(@_);
1a14aa3f 171}
172
8091aa91 173=head2 find_or_create_related
503536d5 174
175 My::Table->find_or_create_related('relname', \%col_data);
176
177=cut
178
55e2d745 179sub find_or_create_related {
180 my $self = shift;
1a14aa3f 181 return $self->find_related(@_) || $self->create_related(@_);
55e2d745 182}
183
8091aa91 184=head2 set_from_related
503536d5 185
186 My::Table->set_from_related('relname', $rel_obj);
187
188=cut
189
55e2d745 190sub set_from_related {
191 my ($self, $rel, $f_obj) = @_;
4685e006 192 my $rel_obj = $self->relationship_info($rel);
701da8c4 193 $self->throw_exception( "No such relationship ${rel}" ) unless $rel_obj;
55e2d745 194 my $cond = $rel_obj->{cond};
701da8c4 195 $self->throw_exception( "set_from_related can only handle a hash condition; the "
55e2d745 196 ."condition for $rel is of type ".(ref $cond ? ref $cond : 'plain scalar'))
197 unless ref $cond eq 'HASH';
7fb16f1a 198 my $f_class = $self->result_source->schema->class($rel_obj->{class});
701da8c4 199 $self->throw_exception( "Object $f_obj isn't a ".$f_class )
55e2d745 200 unless $f_obj->isa($f_class);
201 foreach my $key (keys %$cond) {
202 next if ref $cond->{$key}; # Skip literals and complex conditions
701da8c4 203 $self->throw_exception("set_from_related can't handle $key as key")
55e2d745 204 unless $key =~ m/^foreign\.([^\.]+)$/;
205 my $val = $f_obj->get_column($1);
701da8c4 206 $self->throw_exception("set_from_related can't handle ".$cond->{$key}." as value")
55e2d745 207 unless $cond->{$key} =~ m/^self\.([^\.]+)$/;
208 $self->set_column($1 => $val);
209 }
210 return 1;
211}
212
8091aa91 213=head2 update_from_related
503536d5 214
215 My::Table->update_from_related('relname', $rel_obj);
216
217=cut
218
55e2d745 219sub update_from_related {
220 my $self = shift;
221 $self->set_from_related(@_);
222 $self->update;
223}
224
8091aa91 225=head2 delete_related
503536d5 226
227 My::Table->delete_related('relname', $cond, $attrs);
228
229=cut
230
55e2d745 231sub delete_related {
232 my $self = shift;
233 return $self->search_related(@_)->delete;
234}
235
2361;
237
55e2d745 238=head1 AUTHORS
239
daec44b8 240Matt S. Trout <mst@shadowcatsystems.co.uk>
55e2d745 241
242=head1 LICENSE
243
244You may distribute this code under the same terms as Perl itself.
245
246=cut
247