1 package DBIx::Class::Relationship::Base;
6 use base qw/Class::Data::Inheritable/;
8 __PACKAGE__->mk_classdata('_relationships', { } );
12 DBIx::Class::Relationship - Inter-table relationships
18 This class handles relationships between the tables in your database
19 model. It allows your to set up relationships, and to perform joins
26 =item add_relationship
28 __PACKAGE__->add_relationship('relname', 'Foreign::Class', $cond, $attrs);
30 The condition needs to be an SQL::Abstract-style representation of the
31 join between the tables - for example if you're creating a rel from Foo to Bar
33 { 'foreign.foo_id' => 'self.id' }
35 will result in a JOIN clause like
37 foo me JOIN bar bar ON bar.foo_id = me.id
41 sub add_relationship {
42 my ($class, $rel, $f_class, $cond, $attrs) = @_;
43 die "Can't create relationship without join condition" unless $cond;
46 my %rels = %{ $class->_relationships };
47 $rels{$rel} = { class => $f_class,
50 $class->_relationships(\%rels);
51 #warn %{$f_class->_columns};
53 return unless eval { %{$f_class->_columns}; }; # Foreign class not loaded
54 eval { $class->_resolve_join($rel, 'me') };
56 if ($@) { # If the resolve failed, back out and re-throw the error
58 $class->_relationships(\%rels);
59 $class->throw("Error creating relationship $rel: $@");
65 my ($class, $join, $alias) = @_;
66 if (ref $join eq 'ARRAY') {
67 return map { $class->_resolve_join($_, $alias) } @$join;
68 } elsif (ref $join eq 'HASH') {
69 return map { $class->_resolve_join($_, $alias),
70 $class->_relationships->{$_}{class}->_resolve_join($join->{$_}, $_) }
73 $class->throw("No idea how to resolve join reftype ".ref $join);
75 my $rel_obj = $class->_relationships->{$join};
76 $class->throw("No such relationship ${join}") unless $rel_obj;
77 my $j_class = $rel_obj->{class};
78 my %join = (_action => 'join',
79 _aliases => { 'self' => $alias, 'foreign' => $join },
80 _classes => { $alias => $class, $join => $j_class });
81 my $j_cond = $j_class->resolve_condition($rel_obj->{cond}, \%join);
82 return [ { $join => $j_class->_table_name,
83 -join_type => $rel_obj->{attrs}{join_type} || '' }, $j_cond ];
87 sub resolve_condition {
88 my ($self, $cond, $attrs) = @_;
89 if (ref $cond eq 'HASH') {
91 foreach my $key (keys %$cond) {
92 my $val = $cond->{$key};
94 $self->throw("Can't handle this yet :(");
96 $ret{$self->_cond_key($attrs => $key)}
97 = $self->_cond_value($attrs => $key => $val);
102 $self->throw("Can't handle this yet :(");
107 my ($self, $attrs, $key) = @_;
108 my $action = $attrs->{_action} || '';
109 if ($action eq 'convert') {
110 unless ($key =~ s/^foreign\.//) {
111 $self->throw("Unable to convert relationship to WHERE clause: invalid key ${key}");
114 } elsif ($action eq 'join') {
115 return $key unless $key =~ /\./;
116 my ($type, $field) = split(/\./, $key);
117 if (my $alias = $attrs->{_aliases}{$type}) {
118 my $class = $attrs->{_classes}{$alias};
119 $self->throw("Unknown column $field on $class as $alias")
120 unless exists $class->_columns->{$field};
121 return join('.', $alias, $field);
123 $self->throw( "Unable to resolve type ${type}: only have aliases for ".
124 join(', ', keys %{$attrs->{_aliases} || {}}) );
127 return $self->NEXT::ACTUAL::_cond_key($attrs, $key);
131 my ($self, $attrs, $key, $value) = @_;
132 my $action = $attrs->{_action} || '';
133 if ($action eq 'convert') {
134 unless ($value =~ s/^self\.//) {
135 $self->throw( "Unable to convert relationship to WHERE clause: invalid value ${value}" );
137 unless ($self->_columns->{$value}) {
138 $self->throw( "Unable to convert relationship to WHERE clause: no such accessor ${value}" );
140 return $self->get_column($value);
141 } elsif ($action eq 'join') {
142 return $key unless $key =~ /\./;
143 my ($type, $field) = split(/\./, $value);
144 if (my $alias = $attrs->{_aliases}{$type}) {
145 my $class = $attrs->{_classes}{$alias};
146 $self->throw("Unknown column $field on $class as $alias")
147 unless exists $class->_columns->{$field};
148 return join('.', $alias, $field);
150 $self->throw( "Unable to resolve type ${type}: only have aliases for ".
151 join(', ', keys %{$attrs->{_aliases} || {}}) );
155 return $self->NEXT::ACTUAL::_cond_value($attrs, $key, $value)
160 My::Table->search_related('relname', $cond, $attrs);
166 return $self->_query_related('search', @_);
171 My::Table->count_related('relname', $cond, $attrs);
177 return $self->_query_related('count', @_);
185 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
186 $attrs = { %{ pop(@_) } };
188 my $rel_obj = $self->_relationships->{$rel};
189 $self->throw( "No such relationship ${rel}" ) unless $rel_obj;
190 $attrs = { %{$rel_obj->{attrs} || {}}, %{$attrs || {}} };
192 $self->throw( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1));
193 my $query = ((@_ > 1) ? {@_} : shift);
195 $attrs->{_action} = 'convert'; # shouldn't we resolve the cond to something
196 # to merge into the AST really?
197 my ($cond) = $self->resolve_condition($rel_obj->{cond}, $attrs);
198 $query = ($query ? { '-and' => [ $cond, $query ] } : $cond);
199 #use Data::Dumper; warn Dumper($query);
200 #warn $rel_obj->{class}." $meth $cond ".join(', ', @{$attrs->{bind}||[]});
201 delete $attrs->{_action};
202 return $self->resolve_class($rel_obj->{class}
203 )->$meth($query, $attrs);
208 My::Table->create_related('relname', \%col_data);
214 return $class->new_related(@_)->insert;
219 My::Table->new_related('relname', \%col_data);
224 my ($self, $rel, $values, $attrs) = @_;
225 $self->throw( "Can't call new_related as class method" )
227 $self->throw( "new_related needs a hash" )
228 unless (ref $values eq 'HASH');
229 my $rel_obj = $self->_relationships->{$rel};
230 $self->throw( "No such relationship ${rel}" ) unless $rel_obj;
231 $self->throw( "Can't abstract implicit create for ${rel}, condition not a hash" )
232 unless ref $rel_obj->{cond} eq 'HASH';
233 $attrs = { %{$rel_obj->{attrs}}, %{$attrs || {}}, _action => 'convert' };
235 my %fields = %{$self->resolve_condition($rel_obj->{cond},$attrs)};
236 $fields{$_} = $values->{$_} for keys %$values;
238 return $self->resolve_class($rel_obj->{class})->new(\%fields);
243 My::Table->find_related('relname', @pri_vals | \%pri_vals);
250 my $rel_obj = $self->_relationships->{$rel};
251 $self->throw( "No such relationship ${rel}" ) unless $rel_obj;
252 my ($cond) = $self->resolve_condition($rel_obj->{cond}, { _action => 'convert' });
253 $self->throw( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1));
255 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
256 $attrs = { %{ pop(@_) } };
258 my $query = ((@_ > 1) ? {@_} : shift);
259 $query = ($query ? { '-and' => [ $cond, $query ] } : $cond);
260 return $self->resolve_class($rel_obj->{class})->find($query);
263 =item find_or_create_related
265 My::Table->find_or_create_related('relname', \%col_data);
269 sub find_or_create_related {
271 return $self->find_related(@_) || $self->create_related(@_);
274 =item set_from_related
276 My::Table->set_from_related('relname', $rel_obj);
280 sub set_from_related {
281 my ($self, $rel, $f_obj) = @_;
282 my $rel_obj = $self->_relationships->{$rel};
283 $self->throw( "No such relationship ${rel}" ) unless $rel_obj;
284 my $cond = $rel_obj->{cond};
285 $self->throw( "set_from_related can only handle a hash condition; the "
286 ."condition for $rel is of type ".(ref $cond ? ref $cond : 'plain scalar'))
287 unless ref $cond eq 'HASH';
288 my $f_class = $self->resolve_class($rel_obj->{class});
289 $self->throw( "Object $f_obj isn't a ".$f_class )
290 unless $f_obj->isa($f_class);
291 foreach my $key (keys %$cond) {
292 next if ref $cond->{$key}; # Skip literals and complex conditions
293 $self->throw("set_from_related can't handle $key as key")
294 unless $key =~ m/^foreign\.([^\.]+)$/;
295 my $val = $f_obj->get_column($1);
296 $self->throw("set_from_related can't handle ".$cond->{$key}." as value")
297 unless $cond->{$key} =~ m/^self\.([^\.]+)$/;
298 $self->set_column($1 => $val);
303 =item update_from_related
305 My::Table->update_from_related('relname', $rel_obj);
309 sub update_from_related {
311 $self->set_from_related(@_);
317 My::Table->delete_related('relname', $cond, $attrs);
323 return $self->search_related(@_)->delete;
332 Matt S. Trout <mst@shadowcatsystems.co.uk>
336 You may distribute this code under the same terms as Perl itself.