1 package DBIx::Class::Relationship;
6 use base qw/DBIx::Class/;
8 __PACKAGE__->load_own_components(qw/
18 DBIx::Class::Relationship - Inter-table relationships
24 This class provides methods to set up relationships between the tables
25 in your database model. Relationships are the most useful and powerful
26 technique that L<DBIx::Class> provides. To create efficient database queries,
27 create relationships between any and all tables that have something in
28 common, for example if you have a table Authors:
39 1 | 1 | Rulers of the universe
40 2 | 1 | Rulers of the galaxy
42 Then without relationships, the method of getting all books by Fred goes like
45 my $fred = $schema->resultset('Author')->find({ Name => 'Fred' });
46 my $fredsbooks = $schema->resultset('Book')->search({ Author => $fred->ID });
47 With a has_many relationship called "books" on Author (see below for details),
48 we can do this instead:
50 my $fredsbooks = $schema->resultset('Author')->find({ Name => 'Fred' })->books;
52 Each relationship sets up an accessor method on the
53 L<DBIx::Class::Manual::Glossary/"Row"> objects that represent the items
54 of your table. From L<DBIx::Class::Manual::Glossary/"ResultSet"> objects,
55 the relationships can be searched using the "search_related" method.
56 In list context, each returns a list of Row objects for the related class,
57 in scalar context, a new ResultSet representing the joined tables is
58 returned. Thus, the calls can be chained to produce complex queries.
59 Since the database is not actually queried until you attempt to retrieve
60 the data for an actual item, no time is wasted producing them.
62 my $cheapfredbooks = $schema->resultset('Author')->find({
64 })->books->search_related('prices', {
65 Price => { '<=' => '5.00' },
68 will produce a query something like:
70 SELECT * FROM Author me
71 LEFT JOIN Books books ON books.author = me.id
72 LEFT JOIN Prices prices ON prices.book = books.id
73 WHERE prices.Price <= 5.00
75 all without needing multiple fetches.
77 Only the helper methods for setting up standard relationship types
78 are documented here. For the basic, lower-level methods, see
79 L<DBIx::Class::Relationship::Base>.
83 All helper methods take the following arguments:
85 __PACKAGE__>$method_name('relname', 'Foreign::Class', $cond, $attrs);
87 Both C<$cond> and C<$attrs> are optional. Pass C<undef> for C<$cond> if
88 you want to use the default value for it, but still want to set C<$attrs>.
89 See L<DBIx::Class::Relationship::Base> for a list of valid attributes.
93 # in a Book class (where Author has many Books)
94 My::DBIC::Schema::Book->belongs_to(author => 'Author');
95 my $author_obj = $obj->author;
96 $obj->author($new_author_obj);
98 Creates a relationship where the calling class stores the foreign class's
99 primary key in one (or more) of its columns. If $cond is a column name
100 instead of a join condition hash, that is used as the name of the column
101 holding the foreign key. If $cond is not given, the relname is used as
104 NOTE: If you are used to L<Class::DBI> relationships, this is the equivalent
109 # in an Author class (where Author has many Books)
110 My::DBIC::Schema::Author->has_many(books => 'Book', 'author');
111 my $booklist = $obj->books;
112 my $booklist = $obj->books({
113 name => { LIKE => '%macaroni%' },
114 { prefetch => [qw/book/],
116 my @book_objs = $obj->books;
118 $obj->add_to_books(\%col_data);
120 Creates a one-to-many relationship, where the corresponding elements of the
121 foreign class store the calling class's primary key in one (or more) of its
122 columns. You should pass the name of the column in the foreign class as the
123 $cond argument, or specify a complete join condition.
125 If you delete an object in a class with a C<has_many> relationship, all
126 related objects will be deleted as well. However, any database-level
127 cascade or restrict will take precedence.
131 My::DBIC::Schema::Author->might_have(psuedonym => 'Psuedonyms');
132 my $pname = $obj->psuedonym; # to get the Psuedonym object
134 Creates an optional one-to-one relationship with a class, where the foreign
135 class stores our primary key in one of its columns. Defaults to the primary
136 key of the foreign class unless $cond specifies a column or join condition.
138 If you update or delete an object in a class with a C<might_have>
139 relationship, the related object will be updated or deleted as well.
140 Any database-level update or delete constraints will override this behaviour.
144 My::DBIC::Schema::Book->has_one(isbn => ISBN);
145 my $isbn_obj = $obj->isbn;
147 Creates a one-to-one relationship with another class. This is just like
148 C<might_have>, except the implication is that the other object is always
149 present. The only difference between C<has_one> and C<might_have> is that
150 C<has_one> uses an (ordinary) inner join, whereas C<might_have> uses a
156 My::DBIC::Schema::Actor->many_to_many( roles => 'actor_roles', 'Roles' );
160 my @role_objs = $actor->roles;
162 Creates an accessor bridging two relationships; not strictly a relationship
163 in its own right, although the accessor will return a resultset or collection
164 of objects just as a has_many would.
172 Matt S. Trout <mst@shadowcatsystems.co.uk>
176 You may distribute this code under the same terms as Perl itself.