use strict;
use warnings;
use Try::Tiny;
+use namespace::clean;
-our %_pod_inherit_config =
+our %_pod_inherit_config =
(
class_map => { 'DBIx::Class::Relationship::BelongsTo' => 'DBIx::Class::Relationship' }
);
my ($class, $rel, $f_class, $cond, $attrs) = @_;
# assume a foreign key contraint unless defined otherwise
- $attrs->{is_foreign_key_constraint} = 1
+ $attrs->{is_foreign_key_constraint} = 1
if not exists $attrs->{is_foreign_key_constraint};
$attrs->{undef_on_null_fk} = 1
if not exists $attrs->{undef_on_null_fk};
"$fk is not a column of $class"
) unless $class->has_column($fk);
- my $acc_type = $class->has_column($rel) ? 'filter' : 'single';
- $class->add_relationship($rel, $f_class,
- { "foreign.${pri}" => "self.${fk}" },
- { accessor => $acc_type, %{$attrs || {}} }
- );
+ $cond = { "foreign.${pri}" => "self.${fk}" };
+
}
# explicit join condition
elsif (ref $cond) {
}
$cond = $cond_rel;
}
- my $acc_type = ((ref $cond eq 'HASH')
- && keys %$cond == 1
- && $class->has_column($rel))
- ? 'filter'
- : 'single';
- $class->add_relationship($rel, $f_class,
- $cond,
- { accessor => $acc_type, %{$attrs || {}} }
- );
}
+ # dunno
else {
$class->throw_exception(
'third argument for belongs_to must be undef, a column name, '.
'or a join condition'
);
}
+
+ my $acc_type = (
+ ref $cond eq 'HASH'
+ and
+ keys %$cond == 1
+ and
+ $class->has_column($rel)
+ ) ? 'filter' : 'single';
+
+ my $fk_columns = ($acc_type eq 'single' and ref $cond eq 'HASH')
+ ? { map { $_ =~ /^self\.(.+)/ ? ( $1 => 1 ) : () } (values %$cond ) }
+ : undef
+ ;
+
+ $class->add_relationship($rel, $f_class,
+ $cond,
+ {
+ accessor => $acc_type,
+ $fk_columns ? ( fk_columns => $fk_columns ) : (),
+ %{$attrs || {}}
+ }
+ );
+
return 1;
}