1 package DBIx::Class::Relationship;
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
28 sub add_relationship {
29 my ($class, $rel, $f_class, $cond, $attrs) = @_;
30 my %rels = %{ $class->_relationships };
31 $rels{$rel} = { class => $f_class,
34 $class->_relationships(\%rels);
38 my ($self, $attrs, $key) = @_;
39 my $action = $attrs->{_action} || '';
40 if ($action eq 'convert') {
41 unless ($key =~ s/^foreign\.//) {
42 die "Unable to convert relationship to WHERE clause: invalid key ${key}";
45 } elsif ($action eq 'join') {
46 my ($type, $field) = split(/\./, $key);
47 if ($attrs->{_aliases}{$type}) {
48 return join('.', $attrs->{_aliases}{$type}, $field);
50 die "Unable to resolve type ${type}: only have aliases for ".
51 join(', ', keys %{$attrs->{_aliases}{$type} || {}});
54 return $self->NEXT::ACTUAL::_cond_key($attrs, $key);
58 my ($self, $attrs, $key, $value) = @_;
59 my $action = $attrs->{_action} || '';
60 if ($action eq 'convert') {
61 unless ($value =~ s/^self\.//) {
62 die "Unable to convert relationship to WHERE clause: invalid value ${value}";
64 unless ($self->_columns->{$value}) {
65 die "Unable to convert relationship to WHERE clause: no such accessor ${value}";
67 push(@{$attrs->{bind}}, $self->get_column($value));
69 } elsif ($action eq 'join') {
70 my ($type, $field) = split(/\./, $value);
71 if ($attrs->{_aliases}{$type}) {
72 return join('.', $attrs->{_aliases}{$type}, $field);
74 die "Unable to resolve type ${type}: only have aliases for ".
75 join(', ', keys %{$attrs->{_aliases}{$type} || {}});
79 return $self->NEXT::ACTUAL::_cond_value($attrs, $key, $value)
86 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
87 $attrs = { %{ pop(@_) } };
89 my $rel_obj = $self->_relationships->{$rel};
90 die "No such relationship ${rel}" unless $rel;
91 $attrs = { %{$rel_obj->{attrs} || {}}, %{$attrs || {}} };
94 die "Invalid query: @_" if (@_ > 1 && (@_ % 2 == 1));
95 my $query = ((@_ > 1) ? {@_} : shift);
96 $s_cond = $self->_cond_resolve($query, $attrs);
98 $attrs->{_action} = 'convert';
99 my ($cond) = $self->_cond_resolve($rel_obj->{cond}, $attrs);
100 $cond = "${s_cond} AND ${cond}" if $s_cond;
101 return $rel_obj->{class}->retrieve_from_sql($cond, @{$attrs->{bind} || []},
106 my ($self, $rel, $values, $attrs) = @_;
107 die "Can't call create_related as class method" unless ref $self;
108 die "create_related needs a hash" unless (ref $values eq 'HASH');
109 my $rel_obj = $self->_relationships->{$rel};
110 die "No such relationship ${rel}" unless $rel;
111 die "Can't abstract implicit create for ${rel}, condition not a hash"
112 unless ref $rel_obj->{cond} eq 'HASH';
113 $attrs = { %{$rel_obj->{attrs}}, %{$attrs || {}}, _action => 'convert' };
114 my %fields = %$values;
115 while (my ($k, $v) = each %{$rel_obj->{cond}}) {
116 $self->_cond_value($attrs, $k => $v);
117 $fields{$self->_cond_key($attrs, $k)} = (@{delete $attrs->{bind}})[0];
119 return $rel_obj->{class}->create(\%fields);
128 Matt S. Trout <perl-stuff@trout.me.uk>
132 You may distribute this code under the same terms as Perl itself.