}
# multiple key relationship
else {
- my %f_primaries = %{ $f_class->_primaries };
+ my %f_primaries = eval { %{ $f_class->_primaries } };
+ my $f_loaded = !$@;
my $cond_rel;
for (keys %$cond) {
+ if (m/\./) { # Explicit join condition
+ $cond_rel = $cond;
+ last;
+ }
$cond_rel->{"foreign.$_"} = "self.".$cond->{$_};
# primary key usage checks
if (exists $f_primaries{$_}) {
delete $f_primaries{$_};
- }
- else
- {
+ } elsif ($f_loaded) {
$class->throw("non primary key used in join condition: $_");
}
}
- $class->throw("not all primary keys used in multi key relationship!") if keys %f_primaries;
+ $class->throw("not all primary keys used in multi key relationship!") if $f_loaded && keys %f_primaries;
$class->add_relationship($rel, $f_class,
$cond_rel,
{ accessor => 'single', %{$attrs ||{}} }
sub _has_one {
my ($class, $join_type, $rel, $f_class, $cond, $attrs) = @_;
- unless ($cond) {
+ unless (ref $cond) {
my ($pri, $too_many) = keys %{ $class->_primaries };
$class->throw( "might_have/has_one can only infer join for a single primary key; ${class} has more" )
if $too_many;
my $f_key;
- if ($f_class->_columns->{$rel}) {
+ if ($cond) {
+ $f_key = $cond;
+ } elsif ($f_class->_columns->{$rel}) {
$f_key = $rel;
} else {
($f_key, $too_many) = keys %{ $f_class->_primaries };
sub update {
my ($self, $upd) = @_;
$self->throw( "Not in database" ) unless $self->in_storage;
- my %to_update = %{$upd || {}};
+ if (ref $upd eq 'HASH') {
+ $self->$_($upd->{$_}) for keys %$upd;
+ }
+ my %to_update;
$to_update{$_} = $self->get_column($_) for $self->is_changed;
return -1 unless keys %to_update;
my $rows = $self->storage->update($self->_table_name, \%to_update,