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 my $cascade = delete $args->{cascade} || '';
74 if (delete $args->{no_cascade_delete} || $cascade eq 'None') {
75 $args->{cascade_delete} = 0;
77 elsif( $cascade eq 'Delete' ) {
78 $args->{cascade_delete} = 1;
80 elsif( length $cascade ) {
81 warn "Unemulated cascade option '$cascade' in $class->has_many($rel => $f_class)";
84 if( !$f_key and !@f_method ) {
85 $class->ensure_class_loaded($f_class);
86 my $f_source = $f_class->result_source_instance;
87 ($f_key) = grep { $f_source->relationship_info($_)->{class} eq $class }
88 $f_source->relationships;
91 $class->next::method($rel, $f_class, $f_key, $args);
95 $class->result_source_instance->relationship_info($rel)
100 no warnings 'redefine';
101 my $post_proc = sub { my $o = shift; $o = $o->$_ for @f_method; $o; };
102 *{"${class}::${rel}"} =
104 my $rs = shift->search_related($rel => @_);
105 $rs->{attrs}{record_filter} = $post_proc;
106 return (wantarray ? $rs->all : $rs);
115 my ($class, $rel, $f_class, @columns) = @_;
118 if (ref $columns[0] || !defined $columns[0]) {
119 $ret = $class->next::method($rel, $f_class, @columns);
121 $ret = $class->next::method($rel, $f_class, undef,
122 { proxy => \@columns });
125 $class->_extend_meta(
127 $class->result_source_instance->relationship_info($rel)
135 my ($class, $type, $rel, $val) = @_;
136 my %hash = %{ Clone::clone($class->__meta_info || {}) };
138 $val->{self_class} = $class;
139 $val->{type} = $type;
140 $val->{accessor} = $rel;
142 $hash{$type}{$rel} = DBIx::Class::CDBICompat::Relationship->new($val);
143 $class->__meta_info(\%hash);
148 my ($class, $type, $rel) = @_;
149 my $meta = $class->__meta_info;
150 return $meta unless $type;
152 my $type_meta = $meta->{$type};
153 return $type_meta unless $rel;
154 return $type_meta->{$rel};
161 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
162 $attrs = { %{ pop(@_) } };
164 my $where = (@_ ? ((@_ == 1) ? ((ref $_[0] eq "HASH") ? { %{+shift} } : shift)
167 if (ref $where eq 'HASH') {
168 foreach my $key (keys %$where) { # has_a deflation hack
169 $where->{$key} = ''.$where->{$key}
170 if eval { $where->{$key}->isa('DBIx::Class') };
173 $self->next::method($where, $attrs);