From: Justin Guenther Date: Wed, 23 Aug 2006 17:02:22 +0000 (+0000) Subject: Cleaned up and clarified Relationship docs X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2f3105ce2a484d2c6cb4f514c31b9dea05e9a75c;p=dbsrgits%2FDBIx-Class-Historic.git Cleaned up and clarified Relationship docs --- diff --git a/lib/DBIx/Class/Relationship.pm b/lib/DBIx/Class/Relationship.pm index c6720e9..44da808 100644 --- a/lib/DBIx/Class/Relationship.pm +++ b/lib/DBIx/Class/Relationship.pm @@ -107,6 +107,12 @@ See L for a list of valid attributes. =head2 belongs_to +=over 4 + +=item Arguments: $accessor_name, $related_class, $foreign_key_column?, $attr? + +=back + # in a Book class (where Author has many Books) My::DBIC::Schema::Book->belongs_to(author => 'My::DBIC::Schema::Author'); my $author_obj = $obj->author; @@ -114,17 +120,16 @@ See L for a list of valid attributes. Creates a relationship where the calling class stores the foreign class's primary key in one (or more) of its columns. If C<$cond> is a column name -instead of a join condition hash, that is used as the name of the column -holding the foreign key. If C<$cond> is not given, the relname is used as -the column name. - -If the relationship is optional - i.e. the column containing the foreign -key can be NULL - then the belongs_to relationship does the right -thing - so in the example above C<$obj-Eauthor> would return C. -However in this case you would probably want to set the C -attribute so that a C is done, which makes complex -resultsets involving C or C operations work correctly. -The modified declaration is shown below: +instead of a join condition hash, it is used as the name of the foreign key +column in the calling class. If C<$cond> is not given, C<$accessor_name> is +used as the column name. + +If the relationship is optional -- i.e. the column containing the foreign key +can be NULL -- then the belongs_to relationship does the right thing. Thus, in +the example above C<$obj-Eauthor> would return C. However in this +case you would probably want to set the C attribute so that a C is done, which makes complex resultsets involving C or C +operations work correctly. The modified declaration is shown below: # in a Book class (where Author has_many Books) __PACKAGE__->belongs_to(author => 'My::DBIC::Schema::Author', @@ -140,6 +145,12 @@ of C. =head2 has_many +=over 4 + +=item Arguments: $accessor_name, $related_class, $foreign_key_column?, $attr? + +=back + # in an Author class (where Author has_many Books) My::DBIC::Schema::Author->has_many(books => 'My::DBIC::Schema::Book', 'author'); my $booklist = $obj->books; @@ -159,12 +170,12 @@ columns. You should pass the name of the column in the foreign class as the C<$cond> argument, or specify a complete join condition. Three methods are created when you create a has_many relationship. The first -method is the expected accessor method. The second is almost exactly the same -as the accessor method but "_rs" is added to the end of the method name. This -method works just like the normal accessor, except that it returns a resultset -no matter what, even in list context. The third method, named -C<< add_to_ >>, will also be added to your Row items; this allows -you to insert new related items, using the same mechanism as in +method is the expected accessor method, C<$accessor_name()>. The second is +almost exactly the same as the accessor method but "_rs" is added to the end of +the method name. This method works just like the normal accessor, except that +it returns a resultset no matter what, even in list context. The third method, +named C<< add_to_ >>, will also be added to your Row items; this +allows you to insert new related items, using the same mechanism as in L. If you delete an object in a class with a C relationship, all @@ -175,13 +186,21 @@ DBIx-Class-based cascading delete. =head2 might_have +=over 4 + +=item Arguments: $accessor_name, $related_class, $join_condition?, $attr? + +=back + My::DBIC::Schema::Author->might_have(pseudonym => - 'My::DBIC::Schema::Pseudonyms'); + 'My::DBIC::Schema::Pseudonym'); + my $pname = $obj->pseudonym; # to get the Pseudonym object -Creates an optional one-to-one relationship with a class, where the foreign -class stores our primary key in one of its columns. Defaults to the primary -key of the foreign class unless C<$cond> specifies a column or join condition. +Creates an optional one-to-one relationship with a class. This relationship +defaults to using the relationship name as the foreign key in C<$related_class> +to resolve the join, unless C<$join_condition> specifies a column in +C<$related_class> or a join condition hash reference. If you update or delete an object in a class with a C relationship, the related object will be updated or deleted as well. To @@ -189,10 +208,19 @@ turn off this behavior, add C<< cascade_delete => 0 >> to the C<$attr> hashref. Any database-level update or delete constraints will override this behavior. +In the above example + =head2 has_one +=over 4 + +=item Arguments: $accessor_name, $related_class_name, $join_condition?, $attr? + +=back + My::DBIC::Schema::Book->has_one(isbn => 'My::DBIC::Schema::ISBN'); - my $isbn_obj = $obj->isbn; + + my $isbn_obj = $obj->isbn; # to get the ISBN object Creates a one-to-one relationship with another class. This is just like C, except the implication is that the other object is always @@ -200,15 +228,38 @@ present. The only difference between C and C is that C uses an (ordinary) inner join, whereas C uses a left join. +The has_one relationship should be used when a row in the table has exactly one +related row in another table. If the related row might not exist in the foreign +table, use the L relationship. + +In the above example, each Book in the database is associated with exactly one +ISBN object. =head2 many_to_many =over 4 -=item Arguments: $accessor_name, $link_rel_name, $foreign_rel_name +=item Arguments: + +=over 4 + +=item C<$accessor_name>: The name of the new many_to_many accessor, + +=item C<$link_rel_name>: The accessor name of the has_many relationship from + the current table to the link table, + +=item C<$foreign_rel_name>: The accessor name for the belongs_to relationship + from the link table to the foreign table + +=item C<$attr>: A hash of relationship attributes for the created many_to_many + relationship accessors to use on the C<$foreign_rel_name>. =back +=back + +To create a many_to_many relationship from Actor to Role: + My::DBIC::Schema::Actor->has_many( actor_roles => 'My::DBIC::Schema::ActorRoles', 'actor' ); @@ -220,6 +271,14 @@ left join. My::DBIC::Schema::Actor->many_to_many( roles => 'actor_roles', 'role' ); +And, for the reverse relationship, from Role to Actor: + + My::DBIC::Schema::Role->has_many( actor_roles => + 'My::DBIC::Schema::ActorRoles', + 'role' ); + + My::DBIC::Schema::Role->many_to_many( actors => 'actor_roles', 'actor' ); + Creates accessors bridging two relationships; not strictly a relationship in its own right, although the accessor will return a resultset or collection of objects just as a has_many would. @@ -228,6 +287,11 @@ To use many_to_many, existing relationships from the original table to the link table, and from the link table to the end table must already exist, these relation names are then used in the many_to_many call. +In the above example, the Role class will have 3 many_to_many accessor methods +set: C<$roles>, C<$add_to_roles>, C<$set_roles>, and similarly named accessors +will be created for the Actor class. See L for +more information. + =cut 1;