use DBIx::Class::ResultSet;
use Carp::Clan qw/^DBIx::Class/;
-
use Storable;
-use Scalar::Util qw/weaken/;
use base qw/DBIx::Class/;
__PACKAGE__->load_components(qw/AccessorGroup/);
$source->add_relationship('relname', 'related_source', $cond, $attrs);
-The relation name can be arbitrary, but must be unique for each relationship
-attached to this result source. 'related_source' should be the name with
-which the related result source was registered with the current schema
-(for simple schemas this is usally either Some::Namespace::Foo or just Foo)
+The relationship name can be arbitrary, but must be unique for each
+relationship attached to this result source. 'related_source' should
+be the name with which the related result source was registered with
+the current schema. For example:
+
+ $schema->source('Book')->add_relationship('reviews', 'Review', {
+ 'foreign.book_id' => 'self.id',
+ });
-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 Author to Book,
+The condition C<$cond> needs to be an SQL::Abstract-style
+representation of the join between the tables. For example, if you're
+creating a rel from Author to Book,
{ 'foreign.author_id' => 'self.id' }
=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:
+An arrayref containing a list of accessors in the foreign class to proxy in
+the main class. If, for example, you do the following:
+
+ CD->might_have(liner_notes => 'LinerNotes', undef, {
+ proxy => [ qw/notes/ ],
+ });
+
+Then, assuming LinerNotes has an accessor named notes, you can do:
- my $obj = Foo->find(1);
- $obj->margle(10); # set margle; Bar object is created if it doesn't exist
+ my $cd = CD->find(1);
+ $cd->notes('Notes go here'); # set notes -- LinerNotes object is
+ # created if it doesn't exist
=item accessor
my $rel_info = $self->relationship_info( $pre );
$self->throw_exception( $self->name . " has no such relationship '$pre'" )
unless $rel_info;
- my $as_prefix = ($alias =~ /^.*?\.(.*)$/ ? $1.'.' : '');
+ my $as_prefix = ($alias =~ /^.*?\.(.+)$/ ? $1.'.' : '');
my $rel_source = $self->related_source($pre);
if (exists $rel_info->{attrs}{accessor}
$self->throw_exception(
"Can't prefetch has_many ${pre} (join cond too complex)")
unless ref($rel_info->{cond}) eq 'HASH';
- my @key = map { (/^foreign\.(.*)$/ ? ($1) : ()); }
+ my @key = map { (/^foreign\.(.+)$/ ? ($1) : ()); }
keys %{$rel_info->{cond}};
$collapse->{"${as_prefix}${pre}"} = \@key;
my @ord = (ref($rel_info->{attrs}{order_by}) eq 'ARRAY'
return $self->schema->source($self->relationship_info($rel)->{source});
}
+=head2 related_class
+
+=head3 Arguments: ($relname)
+
+Returns the class object for the given relationship
+
+=cut
+
+sub related_class {
+ my ($self, $rel) = @_;
+ if( !$self->has_relationship( $rel ) ) {
+ $self->throw_exception("No such relationship '$rel'");
+ }
+ return $self->schema->class($self->relationship_info($rel)->{source});
+}
+
=head2 resultset
Returns a resultset for the given source, by calling:
my $self = shift;
$self->throw_exception('resultset does not take any arguments. If you want another resultset, call it on the schema instead.') if scalar @_;
return $self->{_resultset} if ref $self->{_resultset} eq $self->resultset_class;
- return $self->{_resultset} = do {
- my $rs = $self->resultset_class->new($self, $self->{resultset_attributes});
- weaken $rs->result_source;
- $rs;
- };
+ return $self->{_resultset} = $self->resultset_class->new($self, $self->{resultset_attributes});
}
=head2 throw_exception