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