Tweaks to $obj->update and relationship helpers
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / HasOne.pm
1 package DBIx::Class::Relationship::HasOne;
2
3 use strict;
4 use warnings;
5
6 sub might_have {
7   shift->_has_one('LEFT' => @_);
8 }
9
10 sub has_one {
11   shift->_has_one(undef => @_);
12 }
13
14 sub _has_one {
15   my ($class, $join_type, $rel, $f_class, $cond, $attrs) = @_;
16   unless (ref $cond) {
17     my ($pri, $too_many) = keys %{ $class->_primaries };
18     $class->throw( "might_have/has_one can only infer join for a single primary key; ${class} has more" )
19       if $too_many;
20     my $f_key;
21     if ($cond) {
22       $f_key = $cond;
23     } elsif ($f_class->_columns->{$rel}) {
24       $f_key = $rel;
25     } else {
26       ($f_key, $too_many) = keys %{ $f_class->_primaries };
27       $class->throw( "might_have/has_one can only infer join for a single primary key; ${f_class} has more" )
28         if $too_many;
29     }
30     $cond = { "foreign.${f_key}" => "self.${pri}" };
31   }
32   $class->add_relationship($rel, $f_class,
33    $cond,
34    { accessor => 'single',
35      cascade_update => 1, cascade_delete => 1,
36      ($join_type ? ('join_type' => $join_type) : ()),
37      %{$attrs || {}} });
38   1;
39 }
40
41 1;