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 = $self->result_source_instance->relationship_info($col);
62 my ($class, $rel, $f_class, $f_key, $args) = @_;
66 if (ref $f_class eq 'ARRAY') {
67 ($f_class, @f_method) = @$f_class;
70 if (ref $f_key eq 'HASH' && !$args) { $args = $f_key; undef $f_key; };
73 if (delete $args->{no_cascade_delete}) {
74 $args->{cascade_delete} = 0;
77 if( !$f_key and !@f_method ) {
78 my $f_source = $f_class->result_source_instance;
79 ($f_key) = grep { $f_source->relationship_info($_)->{class} eq $class }
80 $f_source->relationships;
83 $class->next::method($rel, $f_class, $f_key, $args);
87 $class->result_source_instance->relationship_info($rel)
92 no warnings 'redefine';
93 my $post_proc = sub { my $o = shift; $o = $o->$_ for @f_method; $o; };
94 *{"${class}::${rel}"} =
96 my $rs = shift->search_related($rel => @_);
97 $rs->{attrs}{record_filter} = $post_proc;
98 return (wantarray ? $rs->all : $rs);
107 my ($class, $rel, $f_class, @columns) = @_;
110 if (ref $columns[0] || !defined $columns[0]) {
111 $ret = $class->next::method($rel, $f_class, @columns);
113 $ret = $class->next::method($rel, $f_class, undef,
114 { proxy => \@columns });
117 $class->_extend_meta(
119 $class->result_source_instance->relationship_info($rel)
127 my ($class, $type, $rel, $val) = @_;
128 my %hash = %{ Clone::clone($class->__meta_info || {}) };
130 $val->{self_class} = $class;
131 $val->{type} = $type;
132 $val->{accessor} = $rel;
134 $hash{$type}{$rel} = DBIx::Class::CDBICompat::Relationship->new($val);
135 $class->__meta_info(\%hash);
139 my ($class, $type, $rel) = @_;
140 my $meta = $class->__meta_info;
141 return $meta unless $type;
143 my $type_meta = $meta->{$type};
144 return $type_meta unless $rel;
145 return $type_meta->{$rel};
152 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
153 $attrs = { %{ pop(@_) } };
155 my $where = (@_ ? ((@_ == 1) ? ((ref $_[0] eq "HASH") ? { %{+shift} } : shift)
158 if (ref $where eq 'HASH') {
159 foreach my $key (keys %$where) { # has_a deflation hack
160 $where->{$key} = ''.$where->{$key}
161 if eval { $where->{$key}->isa('DBIx::Class') };
164 $self->next::method($where, $attrs);