Moved might_have compat back out into a CDBICompat class and documented stuff
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / HasOne.pm
CommitLineData
22b15c96 1package DBIx::Class::Relationship::HasOne;
2
3use strict;
4use warnings;
5
07037f89 6sub might_have {
7 shift->_has_one('LEFT' => @_);
8}
9
22b15c96 10sub has_one {
07037f89 11 shift->_has_one(undef => @_);
12}
13
14sub _has_one {
503536d5 15 my ($class, $join_type, $rel, $f_class, $cond, $attrs) = @_;
16 unless ($cond) {
07037f89 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 ($f_class->_columns->{$rel}) {
22 $f_key = $rel;
23 } else {
24 ($f_key, $too_many) = keys %{ $f_class->_primaries };
25 $class->throw( "might_have/has_one can only infer join for a single primary key; ${f_class} has more" )
26 if $too_many;
22b15c96 27 }
503536d5 28 $cond = { "foreign.${f_key}" => "self.${pri}" };
07037f89 29 }
07037f89 30 $class->add_relationship($rel, $f_class,
31 $cond,
503536d5 32 { accessor => 'single',
07037f89 33 cascade_update => 1, cascade_delete => 1,
34 ($join_type ? ('join_type' => $join_type) : ()),
503536d5 35 %{$attrs || {}} });
07037f89 36 1;
22b15c96 37}
38
391;