fixed up relationship docs
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / Relationship / Base.pm
index 3aa67aa..ba44f0f 100644 (file)
@@ -3,13 +3,13 @@ package DBIx::Class::Relationship::Base;
 use strict;
 use warnings;
 
-use base qw/Class::Data::Inheritable/;
+use base qw/DBIx::Class/;
 
 __PACKAGE__->mk_classdata('_relationships', { } );
 
 =head1 NAME 
 
-DBIx::Class::Relationship - Inter-table relationships
+DBIx::Class::Relationship::Base - Inter-table relationships
 
 =head1 SYNOPSIS
 
@@ -21,36 +21,71 @@ on searches.
 
 =head1 METHODS
 
-=over 4
-
-=item add_relationship
+=head2 add_relationship
 
   __PACKAGE__->add_relationship('relname', 'Foreign::Class', $cond, $attrs);
 
 The condition needs to be an SQL::Abstract-style representation of the
-join between the tables - for example if you're creating a rel from Foo to Bar
+join between the tables. For example, if you're creating a rel from Foo to Bar,
 
   { 'foreign.foo_id' => 'self.id' }
 
-will result in a JOIN clause like
+will result in the JOIN clause
 
   foo me JOIN bar bar ON bar.foo_id = me.id
 
+You can specify as many foreign => self mappings as necessary.
+
+Valid attributes are as follows:
+
+=over 4
+
+=item join_type
+
+Explicitly specifies the type of join to use in the relationship. Any SQL
+join type is valid, e.g. C<LEFT> or C<RIGHT>. It will be placed in the SQL
+command immediately before C<JOIN>.
+
+=item proxy
+
+An arrayref containing a list of accessors in the foreign class to proxy in
+the main class. If, for example, you do the following:
+  
+  __PACKAGE__->might_have(bar => 'Bar', undef, { proxy => qw[/ margle /] });
+  
+Then, assuming Bar has an accessor named margle, you can do:
+
+  my $obj = Foo->find(1);
+  $obj->margle(10); # set margle; Bar object is created if it doesn't exist
+  
+=item accessor
+
+Specifies the type of accessor that should be created for the relationship.
+Valid values are C<single> (for when there is only a single related object),
+C<multi> (when there can be many), and C<filter> (for when there is a single
+related object, but you also want the relationship accessor to double as
+a column accessor). For C<multi> accessors, an add_to_* method is also
+created, which calls C<create_related> for the relationship.
+
+=back
+
 =cut
 
 sub add_relationship {
   my ($class, $rel, $f_class, $cond, $attrs) = @_;
   die "Can't create relationship without join condition" unless $cond;
   $attrs ||= {};
-  eval "use $f_class;";
+  eval "require $f_class;";
+  if ($@) {
+    $class->throw($@) unless $@ =~ /Can't locate/;
+  }
   my %rels = %{ $class->_relationships };
   $rels{$rel} = { class => $f_class,
                   cond  => $cond,
                   attrs => $attrs };
   $class->_relationships(\%rels);
-  #warn %{$f_class->_columns};
 
-  return unless eval { %{$f_class->_columns}; }; # Foreign class not loaded
+  return unless eval { $f_class->can('columns'); }; # Foreign class not loaded
   eval { $class->_resolve_join($rel, 'me') };
 
   if ($@) { # If the resolve failed, back out and re-throw the error
@@ -117,14 +152,14 @@ sub _cond_key {
     if (my $alias = $attrs->{_aliases}{$type}) {
       my $class = $attrs->{_classes}{$alias};
       $self->throw("Unknown column $field on $class as $alias")
-        unless exists $class->_columns->{$field};
+        unless $class->has_column($field);
       return join('.', $alias, $field);
     } else {
       $self->throw( "Unable to resolve type ${type}: only have aliases for ".
             join(', ', keys %{$attrs->{_aliases} || {}}) );
     }
   }
-  return $self->NEXT::ACTUAL::_cond_key($attrs, $key);
+  return $self->next::method($attrs, $key);
 }
 
 sub _cond_value {
@@ -134,7 +169,7 @@ sub _cond_value {
     unless ($value =~ s/^self\.//) {
       $self->throw( "Unable to convert relationship to WHERE clause: invalid value ${value}" );
     }
-    unless ($self->_columns->{$value}) {
+    unless ($self->has_column($value)) {
       $self->throw( "Unable to convert relationship to WHERE clause: no such accessor ${value}" );
     }
     return $self->get_column($value);
@@ -144,7 +179,7 @@ sub _cond_value {
     if (my $alias = $attrs->{_aliases}{$type}) {
       my $class = $attrs->{_classes}{$alias};
       $self->throw("Unknown column $field on $class as $alias")
-        unless exists $class->_columns->{$field};
+        unless $class->has_column($field);
       return join('.', $alias, $field);
     } else {
       $self->throw( "Unable to resolve type ${type}: only have aliases for ".
@@ -152,10 +187,10 @@ sub _cond_value {
     }
   }
       
-  return $self->NEXT::ACTUAL::_cond_value($attrs, $key, $value)
+  return $self->next::method($attrs, $key, $value)
 }
 
-=item search_related
+=head2 search_related
 
   My::Table->search_related('relname', $cond, $attrs);
 
@@ -166,7 +201,7 @@ sub search_related {
   return $self->_query_related('search', @_);
 }
 
-=item count_related
+=head2 count_related
 
   My::Table->count_related('relname', $cond, $attrs);
 
@@ -203,7 +238,7 @@ sub _query_related {
            )->$meth($query, $attrs);
 }
 
-=item create_related
+=head2 create_related
 
   My::Table->create_related('relname', \%col_data);
 
@@ -214,7 +249,7 @@ sub create_related {
   return $class->new_related(@_)->insert;
 }
 
-=item new_related
+=head2 new_related
 
   My::Table->new_related('relname', \%col_data);
 
@@ -238,7 +273,7 @@ sub new_related {
   return $self->resolve_class($rel_obj->{class})->new(\%fields);
 }
 
-=item find_related
+=head2 find_related
 
   My::Table->find_related('relname', @pri_vals | \%pri_vals);
 
@@ -260,7 +295,7 @@ sub find_related {
   return $self->resolve_class($rel_obj->{class})->find($query);
 }
 
-=item find_or_create_related
+=head2 find_or_create_related
 
   My::Table->find_or_create_related('relname', \%col_data);
 
@@ -271,7 +306,7 @@ sub find_or_create_related {
   return $self->find_related(@_) || $self->create_related(@_);
 }
 
-=item set_from_related
+=head2 set_from_related
 
   My::Table->set_from_related('relname', $rel_obj);
 
@@ -300,7 +335,7 @@ sub set_from_related {
   return 1;
 }
 
-=item update_from_related
+=head2 update_from_related
 
   My::Table->update_from_related('relname', $rel_obj);
 
@@ -312,7 +347,7 @@ sub update_from_related {
   $self->update;
 }
 
-=item delete_related
+=head2 delete_related
 
   My::Table->delete_related('relname', $cond, $attrs);
 
@@ -325,8 +360,6 @@ sub delete_related {
 
 1;
 
-=back
-
 =head1 AUTHORS
 
 Matt S. Trout <mst@shadowcatsystems.co.uk>