1578c63208a0213e7b5206bad6bb8167ff648687
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / HasOne.pm
1 package # hide from PAUSE
2     DBIx::Class::Relationship::HasOne;
3
4 use strict;
5 use warnings;
6 use Carp::Clan qw/^DBIx::Class/;
7
8 our %_pod_inherit_config = 
9   (
10    class_map => { 'DBIx::Class::Relationship::HasOne' => 'DBIx::Class::Relationship' }
11   );
12
13 sub might_have {
14   shift->_has_one('LEFT' => @_);
15 }
16
17 sub has_one {
18   shift->_has_one(undef() => @_);
19 }
20
21 sub _has_one {
22   my ($class, $join_type, $rel, $f_class, $cond, $attrs) = @_;
23   unless (ref $cond) {
24     $class->ensure_class_loaded($f_class);
25
26     my $pri = $class->_get_primary_key;
27   
28     $class->throw_exception(
29       "might_have/has_one needs a primary key  to infer a join; ".
30       "${class} has none"
31     ) if !defined $pri && (!defined $cond || !length $cond);
32
33     my $f_class_loaded = eval { $f_class->columns };
34     my ($f_key,$too_many,$guess);
35     if (defined $cond && length $cond) {
36       $f_key = $cond;
37       $guess = "caller specified foreign key '$f_key'";
38     } elsif ($f_class_loaded && $f_class->has_column($rel)) {
39       $f_key = $rel;
40       $guess = "using given relationship '$rel' for foreign key";
41     } else {
42       $f_key = $class->_get_primary_key($f_class);
43       $guess = "using primary key of foreign class for foreign key";
44     }
45     $class->throw_exception(
46       "No such column ${f_key} on foreign class ${f_class} ($guess)"
47     ) if $f_class_loaded && !$f_class->has_column($f_key);
48     $cond = { "foreign.${f_key}" => "self.${pri}" };
49   }
50   $class->_validate_cond($cond);
51   $class->add_relationship($rel, $f_class,
52    $cond,
53    { accessor => 'single',
54      cascade_update => 1, cascade_delete => 1,
55      ($join_type ? ('join_type' => $join_type) : ()),
56      %{$attrs || {}} });
57   1;
58 }
59
60 sub _get_primary_key {
61   my ( $class, $target_class ) = @_;
62   $target_class ||= $class;
63   my ($pri, $too_many) = $target_class->primary_columns;
64   $class->throw_exception(
65     "might_have/has_one can only infer join for a single primary key; ".
66     "${class} has more"
67   ) if $too_many;
68   return $pri;
69 }
70
71 sub _validate_cond {
72   my ($class, $cond )  = @_;
73
74   return if $ENV{DBIC_DONT_VALIDATE_RELS};
75   return unless 'HASH' eq ref $cond;
76   foreach my $foreign_id ( keys %$cond ) {
77     my $self_id = $cond->{$foreign_id};
78
79     # we can ignore a bad $self_id because add_relationship handles this
80     # warning
81     return unless $self_id =~ /^self\.(.*)$/;
82     my $key = $1;
83     my $column_info = $class->column_info($key);
84     if ( $column_info->{is_nullable} ) {
85       carp(qq'"might_have/has_one" must not be on columns with is_nullable set to true ($class/$key). This might indicate an incorrect use of those relationship helpers instead of belongs_to.');
86     }
87   }
88 }
89
90 1;