1 package # hide from PAUSE
2 DBIx::Class::CDBICompat::Relationships;
7 use base qw/Class::Data::Inheritable/;
10 use DBIx::Class::CDBICompat::Relationship;
12 __PACKAGE__->mk_classdata('__meta_info' => {});
17 DBIx::Class::CDBICompat::Relationships
21 Emulate C<has_a>, C<has_many>, C<might_have> and C<meta_info>.
26 my ($self, $col, $f_class, %args) = @_;
27 $self->throw_exception( "No such column ${col}" ) unless $self->has_column($col);
28 $self->ensure_class_loaded($f_class);
32 if ($args{'inflate'} || $args{'deflate'}) { # Non-database has_a
33 if (!ref $args{'inflate'}) {
34 my $meth = $args{'inflate'};
35 $args{'inflate'} = sub { $f_class->$meth(shift); };
37 if (!ref $args{'deflate'}) {
38 my $meth = $args{'deflate'};
39 $args{'deflate'} = sub { shift->$meth; };
41 $self->inflate_column($col, \%args);
48 $self->belongs_to($col, $f_class);
49 $rel_info = $self->result_source_instance->relationship_info($col);
52 $rel_info->{args} = \%args;
64 my ($class, $rel, $f_class, $f_key, $args) = @_;
68 if (ref $f_class eq 'ARRAY') {
69 ($f_class, @f_method) = @$f_class;
72 if (ref $f_key eq 'HASH' && !$args) { $args = $f_key; undef $f_key; };
75 my $cascade = delete $args->{cascade} || '';
76 if (delete $args->{no_cascade_delete} || $cascade eq 'None') {
77 $args->{cascade_delete} = 0;
79 elsif( $cascade eq 'Delete' ) {
80 $args->{cascade_delete} = 1;
82 elsif( length $cascade ) {
83 warn "Unemulated cascade option '$cascade' in $class->has_many($rel => $f_class)";
86 if( !$f_key and !@f_method ) {
87 $class->ensure_class_loaded($f_class);
88 my $f_source = $f_class->result_source_instance;
89 ($f_key) = grep { $f_source->relationship_info($_)->{class} eq $class }
90 $f_source->relationships;
93 $class->next::method($rel, $f_class, $f_key, $args);
95 my $rel_info = $class->result_source_instance->relationship_info($rel);
96 $args->{mapping} = \@f_method;
97 $args->{foreign_key} = $f_key;
98 $rel_info->{args} = $args;
100 $class->_extend_meta(
107 no warnings 'redefine';
108 my $post_proc = sub { my $o = shift; $o = $o->$_ for @f_method; $o; };
109 *{"${class}::${rel}"} =
111 my $rs = shift->search_related($rel => @_);
112 $rs->{attrs}{record_filter} = $post_proc;
113 return (wantarray ? $rs->all : $rs);
122 my ($class, $rel, $f_class, @columns) = @_;
125 if (ref $columns[0] || !defined $columns[0]) {
126 $ret = $class->next::method($rel, $f_class, @columns);
128 $ret = $class->next::method($rel, $f_class, undef,
129 { proxy => \@columns });
132 my $rel_info = $class->result_source_instance->relationship_info($rel);
133 $rel_info->{args}{import} = \@columns;
135 $class->_extend_meta(
145 my ($class, $type, $rel, $val) = @_;
146 my %hash = %{ Clone::clone($class->__meta_info || {}) };
148 $val->{self_class} = $class;
149 $val->{type} = $type;
150 $val->{accessor} = $rel;
152 $hash{$type}{$rel} = DBIx::Class::CDBICompat::Relationship->new($val);
153 $class->__meta_info(\%hash);
158 my ($class, $type, $rel) = @_;
159 my $meta = $class->__meta_info;
160 return $meta unless $type;
162 my $type_meta = $meta->{$type};
163 return $type_meta unless $rel;
164 return $type_meta->{$rel};
171 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
172 $attrs = { %{ pop(@_) } };
174 my $where = (@_ ? ((@_ == 1) ? ((ref $_[0] eq "HASH") ? { %{+shift} } : shift)
177 if (ref $where eq 'HASH') {
178 foreach my $key (keys %$where) { # has_a deflation hack
179 $where->{$key} = ''.$where->{$key}
180 if eval { $where->{$key}->isa('DBIx::Class') };
183 $self->next::method($where, $attrs);