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 handles relationships between the tables in your database
25 model. It allows you to set up relationships and perform joins on them.
27 Only the helper methods for setting up standard relationship types
28 are documented here. For the basic, lower-level methods, see
29 L<DBIx::Class::Relationship::Base>.
33 All helper methods take the following arguments:
35 __PACKAGE__>$method_name('relname', 'Foreign::Class', $cond, $attrs);
37 Both C<$cond> and C<$attrs> are optional. Pass C<undef> for C<$cond> if
38 you want to use the default value for it, but still want to set C<$attrs>.
39 See L<DBIx::Class::Relationship::Base> for a list of valid attributes.
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);
48 Creates a relationship where the calling class stores the foreign class's
49 primary key in one (or more) of its columns. If $cond is a column name
50 instead of a join condition hash, that is used as the name of the column
51 holding the foreign key. If $cond is not given, the relname is used as
54 NOTE: If you are used to L<Class::DBI> relationships, this is the equivalent
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;
65 $obj->add_to_books(\%col_data);
67 Creates a one-to-many relationship, where the corresponding elements of the
68 foreign class store the calling class's primary key in one (or more) of its
69 columns. You should pass the name of the column in the foreign class as the
70 $cond argument, or specify a complete join condition.
72 If you delete an object in a class with a C<has_many> relationship, all
73 related objects will be deleted as well. However, any database-level
74 cascade or restrict will take precedence.
78 My::DBIC::Schema::Author->might_have(psuedonym => 'Psuedonyms');
79 my $pname = $obj->psuedonym; # to get the Psuedonym object
81 Creates an optional one-to-one relationship with a class, where the foreign
82 class stores our primary key in one of its columns. Defaults to the primary
83 key of the foreign class unless $cond specifies a column or join condition.
85 If you update or delete an object in a class with a C<might_have>
86 relationship, the related object will be updated or deleted as well.
87 Any database-level update or delete constraints will override this behaviour.
91 My::DBIC::Schema::Book->has_one(isbn => ISBN);
92 my $isbn_obj = $obj->isbn;
94 Creates a one-to-one relationship with another class. This is just like
95 C<might_have>, except the implication is that the other object is always
96 present. The only difference between C<has_one> and C<might_have> is that
97 C<has_one> uses an (ordinary) inner join, whereas C<might_have> uses a
103 My::DBIC::Schema::Actor->many_to_many( roles => 'actor_roles', 'Roles' );
104 my @role_objs = $obj_a->roles;
106 Creates an accessor bridging two relationships; not strictly a relationship
107 in its own right, although the accessor will return a resultset or collection
108 of objects just as a has_many would.
116 Matt S. Trout <mst@shadowcatsystems.co.uk>
120 You may distribute this code under the same terms as Perl itself.