Fix exception text for nonexistent key in ResultSet::find()
[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
34d52be2 16=head1 NAME
17
18DBIx::Class::Relationship - Inter-table relationships
19
20=head1 SYNOPSIS
21
22=head1 DESCRIPTION
23
24This class handles relationships between the tables in your database
bfab575a 25model. It allows you to set up relationships and perform joins on them.
34d52be2 26
bfab575a 27Only the helper methods for setting up standard relationship types
28are documented here. For the basic, lower-level methods, see
29L<DBIx::Class::Relationship::Base>.
503536d5 30
34d52be2 31=head1 METHODS
32
bfab575a 33All helper methods take the following arguments:
503536d5 34
8091aa91 35 __PACKAGE__>$method_name('relname', 'Foreign::Class', $cond, $attrs);
bfab575a 36
37Both C<$cond> and C<$attrs> are optional. Pass C<undef> for C<$cond> if
38you want to use the default value for it, but still want to set C<$attrs>.
8091aa91 39See L<DBIx::Class::Relationship::Base> for a list of valid attributes.
503536d5 40
bfab575a 41=head2 belongs_to
503536d5 42
c99393ff 43 # in a Book class (where Author has many Books)
44 My::DBIC::Schema::Book->belongs_to(author => 'Author');
45 my $author_obj = $obj->author;
46 $obj->author($new_author_obj);
503536d5 47
8091aa91 48Creates a relationship where the calling class stores the foreign class's
49primary key in one (or more) of its columns. If $cond is a column name
50instead of a join condition hash, that is used as the name of the column
51holding the foreign key. If $cond is not given, the relname is used as
52the column name.
bfab575a 53
8091aa91 54NOTE: If you are used to L<Class::DBI> relationships, this is the equivalent
55of C<has_a>.
503536d5 56
bfab575a 57=head2 has_many
503536d5 58
c99393ff 59 # in an Author class (where Author has many Books)
60 My::DBIC::Schema::Author->has_many(books => 'Book', 'author');
61 my $booklist = $obj->books;
62 my $booklist = $obj->books({ name => { LIKE => '%macaroni%' }, { prefetch => [qw/book/] });
63 my @book_objs = $obj->books;
503536d5 64
c99393ff 65 $obj->add_to_books(\%col_data);
503536d5 66
8091aa91 67Creates a one-to-many relationship, where the corresponding elements of the
68foreign class store the calling class's primary key in one (or more) of its
69columns. You should pass the name of the column in the foreign class as the
70$cond argument, or specify a complete join condition.
71
72If you delete an object in a class with a C<has_many> relationship, all
73related objects will be deleted as well. However, any database-level
74cascade or restrict will take precedence.
503536d5 75
bfab575a 76=head2 might_have
503536d5 77
c99393ff 78 My::DBIC::Schema::Author->might_have(psuedonym => 'Psuedonyms');
79 my $pname = $obj->psuedonym; # to get the Psuedonym object
8091aa91 80
c99393ff 81Creates an optional one-to-one relationship with a class, where the foreign
82class stores our primary key in one of its columns. Defaults to the primary
83key of the foreign class unless $cond specifies a column or join condition.
503536d5 84
c99393ff 85If you update or delete an object in a class with a C<might_have>
86relationship, the related object will be updated or deleted as well.
87Any database-level update or delete constraints will override this behaviour.
503536d5 88
bfab575a 89=head2 has_one
90
c99393ff 91 My::DBIC::Schema::Book->has_one(isbn => ISBN);
92 my $isbn_obj = $obj->isbn;
bfab575a 93
c99393ff 94Creates a one-to-one relationship with another class. This is just like
95C<might_have>, except the implication is that the other object is always
96present. The only difference between C<has_one> and C<might_have> is that
97C<has_one> uses an (ordinary) inner join, whereas C<might_have> uses a
98left join.
503536d5 99
7411204b 100
87c4e602 101=head2 many_to_many
102
c99393ff 103 My::DBIC::Schema::Actor->many_to_many( roles => 'actor_roles', 'Roles' );
104 my @role_objs = $obj_a->roles;
b8eca5ce 105
106Creates an accessor bridging two relationships; not strictly a relationship
107in its own right, although the accessor will return a resultset or collection
108of objects just as a has_many would.
7411204b 109
34d52be2 110=cut
111
b8e1e21f 1121;
34d52be2 113
34d52be2 114=head1 AUTHORS
115
daec44b8 116Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 117
118=head1 LICENSE
119
120You may distribute this code under the same terms as Perl itself.
121
122=cut
123