}
}
- # this is where we're going to check if we have an extended
- # rel. In that case, we need to: 1) If there's a second
- # condition, we use that instead. 2) If there is only one
- # condition, we need to join the current resultsource and have
- # additional conditions.
- if (ref $rel_info->{cond} eq 'CODE') {
- # this is an extended relationship.
- if ($extended_cond) {
+ if (ref $rel_info->{cond} eq 'CODE' && !$extended_cond) {
+ # since we don't have the extended condition, we need to step
+ # back, get a resultset for the current row and do a
+ # search_related there.
+
+ my $row_srcname = $source->source_name;
+ my $base_rs_class = $source->resultset_class;
+ my $base_rs_attr = $source->resultset_attributes;
+ my $base_rs = $base_rs_class->new
+ ($source,
+ {
+ %$base_rs_attr,
+ alias => $source->storage->relname_to_table_alias(lc($row_srcname).'__row',1)
+ });
+ my $alias = $base_rs->current_source_alias;
+ my %identity = map { ( "${alias}.${_}" => $self->get_column($_) ) } $source->primary_columns;
+ my $row_rs = $base_rs->search(\%identity);
+ my $related = $row_rs->related_resultset($rel, { %$attrs, alias => 'me' });
+ $related->search($query);
+
+ } else {
+ # when we have the extended condition or we have a simple
+ # relationship declaration, it can optimize the JOIN away by
+ # simply adding the identity in WHERE.
+
+ if (ref $rel_info->{cond} eq 'CODE' && $extended_cond) {
$cond = $extended_cond;
-
- } else {
-
- # it's a bit hard to find out what to do with other joins
- $self->throw_exception('Extended relationship '.$rel.' with additional join requires optimized declaration')
- if exists $attrs->{join} && $attrs->{join};
-
- # aliases get a bit more complicated, so we won't accept additional queries
- $self->throw_exception('Extended relationship '.$rel.' with additional query requires optimized declaration')
- if $query;
-
- $attrs->{from} =
- [ { $rel => $self->result_source->from },
- [ { 'me' => $self->result_source->related_source($rel)->from }, { 1 => 1 } ] ];
-
- $cond->{"${rel}.${_}"} = $self->get_column($_) for $self->result_source->primary_columns;
}
- }
- if (ref $cond eq 'ARRAY') {
- $cond = [ map {
- if (ref $_ eq 'HASH') {
- my $hash;
- foreach my $key (keys %$_) {
- my $newkey = $key !~ /\./ ? "me.$key" : $key;
- $hash->{$newkey} = $_->{$key};
+ if (ref $cond eq 'ARRAY') {
+ $cond = [ map {
+ if (ref $_ eq 'HASH') {
+ my $hash;
+ foreach my $key (keys %$_) {
+ my $newkey = $key !~ /\./ ? "me.$key" : $key;
+ $hash->{$newkey} = $_->{$key};
+ }
+ $hash;
+ } else {
+ $_;
}
- $hash;
- } else {
- $_;
+ } @$cond ];
+ } elsif (ref $cond eq 'HASH') {
+ foreach my $key (grep { ! /\./ } keys %$cond) {
+ $cond->{"me.$key"} = delete $cond->{$key};
}
- } @$cond ];
- } elsif (ref $cond eq 'HASH') {
- foreach my $key (grep { ! /\./ } keys %$cond) {
- $cond->{"me.$key"} = delete $cond->{$key};
}
- }
- $query = ($query ? { '-and' => [ $cond, $query ] } : $cond);
- $self->result_source->related_source($rel)->resultset->search(
- $query, $attrs
- );
+ $query = ($query ? { '-and' => [ $cond, $query ] } : $cond);
+ $self->result_source->related_source($rel)->resultset->search(
+ $query, $attrs);
+ }
};
}
sub create_related {
my $self = shift;
my $rel = shift;
+
+ $self->throw_exception("Can't call *_related as class methods")
+ unless ref $self;
+
+ # we need to stop and check if this is at all possible. If this is
+ # an extended relationship with an incomplete definition, we should
+ # just forbid it right now.
+ my $rel_info = $self->result_source->relationship_info($rel);
+ if (ref $rel_info->{cond} eq 'CODE') {
+ my ($cond, $ext) = $rel_info->{cond}->({ self_alias => 'me',
+ foreign_alias => $rel,
+ self_rowobj => $self
+ });
+ $self->throw_exception("unable to set_from_related - no simplified condition available for '${rel}'")
+ unless $ext;
+
+ # now we need to make sure all non-identity relationship
+ # definitions are overriden.
+ my ($argref) = @_;
+ while ( my($col, $value) = each %$ext ) {
+ $col =~ s/^$rel\.//;
+ my $vref = ref $value;
+ if ($vref eq 'HASH') {
+ if (keys(%$value) && (keys %$value)[0] ne '=' &&
+ !exists $argref->{$col}) {
+ $self->throw_exception("unable to set_from_related via complex '${rel}' condition on column(s): '${col}'")
+ }
+ }
+ }
+ }
+
my $obj = $self->search_related($rel)->create(@_);
delete $self->{related_resultsets}->{$rel};
return $obj;