Added ->relationships and ->relationship_info from castaway
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship.pm
CommitLineData
b8e1e21f 1package DBIx::Class::Relationship;
2
3use strict;
4use warnings;
5
1edd1722 6use base qw/DBIx::Class/;
55e2d745 7
07037f89 8__PACKAGE__->load_own_components(qw/
7411204b 9 Helpers
07037f89 10 Accessor
11 CascadeActions
12 ProxyMethods
13 Base
14/);
b8e1e21f 15
16__PACKAGE__->mk_classdata('_relationships', { } );
17
4685e006 18sub relationships
19{
20 my $self = shift;
21
22 return keys %{$self->_relationships};
23}
24
25sub relationship_info
26{
27 my $self = shift;
28 my ($rel) = @_;
29
30 return $self->_relationships->{$rel};
31}
32
34d52be2 33=head1 NAME
34
35DBIx::Class::Relationship - Inter-table relationships
36
37=head1 SYNOPSIS
38
39=head1 DESCRIPTION
40
41This class handles relationships between the tables in your database
bfab575a 42model. It allows you to set up relationships and perform joins on them.
34d52be2 43
bfab575a 44Only the helper methods for setting up standard relationship types
45are documented here. For the basic, lower-level methods, see
46L<DBIx::Class::Relationship::Base>.
503536d5 47
34d52be2 48=head1 METHODS
49
bfab575a 50All helper methods take the following arguments:
503536d5 51
8091aa91 52 __PACKAGE__>$method_name('relname', 'Foreign::Class', $cond, $attrs);
bfab575a 53
54Both C<$cond> and C<$attrs> are optional. Pass C<undef> for C<$cond> if
55you want to use the default value for it, but still want to set C<$attrs>.
8091aa91 56See L<DBIx::Class::Relationship::Base> for a list of valid attributes.
503536d5 57
bfab575a 58=head2 belongs_to
503536d5 59
8091aa91 60 # in a Bar class (where Foo has many Bars)
61 __PACKAGE__->belongs_to(foo => Foo);
62 my $f_obj = $obj->foo;
63 $obj->foo($new_f_obj);
503536d5 64
8091aa91 65Creates a relationship where the calling class stores the foreign class's
66primary key in one (or more) of its columns. If $cond is a column name
67instead of a join condition hash, that is used as the name of the column
68holding the foreign key. If $cond is not given, the relname is used as
69the column name.
bfab575a 70
8091aa91 71NOTE: If you are used to L<Class::DBI> relationships, this is the equivalent
72of C<has_a>.
503536d5 73
bfab575a 74=head2 has_many
503536d5 75
8091aa91 76 # in a Foo class (where Foo has many Bars)
77 __PACKAGE__->has_many(bar => Bar, 'foo');
78 my $f_resultset = $obj->foo;
79 my $f_resultset = $obj->foo({ name => { LIKE => '%macaroni%' }, { prefetch => [qw/bar/] });
80 my @f_obj = $obj->foo;
503536d5 81
8091aa91 82 $obj->add_to_foo(\%col_data);
503536d5 83
8091aa91 84Creates a one-to-many relationship, where the corresponding elements of the
85foreign class store the calling class's primary key in one (or more) of its
86columns. You should pass the name of the column in the foreign class as the
87$cond argument, or specify a complete join condition.
88
89If you delete an object in a class with a C<has_many> relationship, all
90related objects will be deleted as well. However, any database-level
91cascade or restrict will take precedence.
503536d5 92
bfab575a 93=head2 might_have
503536d5 94
8091aa91 95 __PACKAGE__->might_have(baz => Baz);
96 my $f_obj = $obj->baz; # to get the baz object
97
98Creates an optional one-to-one relationship with a class, where the foreign class
99stores our primary key in one of its columns. Defaults to the primary key of the
100foreign class unless $cond specifies a column or join condition.
503536d5 101
8091aa91 102If you update or delete an object in a class with a C<might_have> relationship,
103the related object will be updated or deleted as well. Any database-level update
104or delete constraints will override this behavior.
503536d5 105
bfab575a 106=head2 has_one
107
8091aa91 108 __PACKAGE__->has_one(gorch => Gorch);
109 my $f_obj = $obj->gorch;
bfab575a 110
8091aa91 111Creates a one-to-one relationship with another class. This is just like C<might_have>,
112except the implication is that the other object is always present. The only different
113between C<has_one> and C<might_have> is that C<has_one> uses an (ordinary) inner join,
114whereas C<might_have> uses a left join.
503536d5 115
7411204b 116
117=head2 many_to_many
118
119 __PACKAGE__->many_to_many( 'accessorname' => 'a_to_b', 'table_b' );
120 my @f_objs = $obj_a->accessorname;
121
34d52be2 122=cut
123
b8e1e21f 1241;
34d52be2 125
34d52be2 126=head1 AUTHORS
127
daec44b8 128Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 129
130=head1 LICENSE
131
132You may distribute this code under the same terms as Perl itself.
133
134=cut
135