From: Matt S Trout Date: Fri, 23 Sep 2005 13:08:47 +0000 (+0000) Subject: Tweaks to $obj->update and relationship helpers X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=99be059e6d635374f075f430af6e7cd6ee18ab67;p=dbsrgits%2FDBIx-Class-Historic.git Tweaks to $obj->update and relationship helpers --- diff --git a/lib/DBIx/Class/Relationship/BelongsTo.pm b/lib/DBIx/Class/Relationship/BelongsTo.pm index cda5fb8..4201835 100644 --- a/lib/DBIx/Class/Relationship/BelongsTo.pm +++ b/lib/DBIx/Class/Relationship/BelongsTo.pm @@ -17,20 +17,23 @@ sub belongs_to { } # 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 ||{}} } diff --git a/lib/DBIx/Class/Relationship/HasOne.pm b/lib/DBIx/Class/Relationship/HasOne.pm index 9f821a4..f3521e6 100644 --- a/lib/DBIx/Class/Relationship/HasOne.pm +++ b/lib/DBIx/Class/Relationship/HasOne.pm @@ -13,12 +13,14 @@ sub has_one { 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 }; diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index f789b29..771467d 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -104,7 +104,10 @@ UPDATE query to commit any changes to the object to the db if required. 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,