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