1 package # hide from PAUSE
2 DBIx::Class::CDBICompat::Relationships;
6 use base 'Class::Data::Inheritable';
9 use DBIx::Class::CDBICompat::Relationship;
10 use DBIx::Class::_Util qw(quote_sub perlstring);
12 __PACKAGE__->mk_classdata('__meta_info' => {});
17 DBIx::Class::CDBICompat::Relationships - Emulate has_a(), has_many(), might_have() and meta_info()
21 Emulate C<has_a>, C<has_many>, C<might_have> and C<meta_info>.
26 my($self, $col, @rest) = @_;
28 $self->_declare_has_a($col, @rest);
29 $self->_mk_inflated_column_accessor($col);
36 my ($self, $col, $f_class, %args) = @_;
37 $self->throw_exception( "No such column ${col}" )
38 unless $self->has_column($col);
39 $self->ensure_class_loaded($f_class);
43 if ($args{'inflate'} || $args{'deflate'}) { # Non-database has_a
44 if (!ref $args{'inflate'}) {
45 my $meth = $args{'inflate'};
46 $args{'inflate'} = sub { $f_class->$meth(shift); };
48 if (!ref $args{'deflate'}) {
49 my $meth = $args{'deflate'};
50 $args{'deflate'} = sub { shift->$meth; };
52 $self->inflate_column($col, \%args);
59 $self->belongs_to($col, $f_class);
60 $rel_info = $self->result_source_instance->relationship_info($col);
63 $rel_info->{args} = \%args;
73 sub _mk_inflated_column_accessor {
74 my($class, $col) = @_;
76 return $class->mk_group_accessors('inflated_column' => $col);
80 my ($class, $rel, $f_class, $f_key, $args) = @_;
84 if (ref $f_class eq 'ARRAY') {
85 ($f_class, @f_method) = @$f_class;
88 if (ref $f_key eq 'HASH' && !$args) { $args = $f_key; undef $f_key; };
91 my $cascade = delete $args->{cascade} || '';
92 if (delete $args->{no_cascade_delete} || $cascade eq 'None') {
93 $args->{cascade_delete} = 0;
95 elsif( $cascade eq 'Delete' ) {
96 $args->{cascade_delete} = 1;
98 elsif( length $cascade ) {
99 warn "Unemulated cascade option '$cascade' in $class->has_many($rel => $f_class)";
102 if( !$f_key and !@f_method ) {
103 $class->ensure_class_loaded($f_class);
104 my $f_source = $f_class->result_source_instance;
105 ($f_key) = grep { $f_source->relationship_info($_)->{class} eq $class }
106 $f_source->relationships;
109 $class->next::method($rel, $f_class, $f_key, $args);
111 my $rel_info = $class->result_source_instance->relationship_info($rel);
112 $args->{mapping} = \@f_method;
113 $args->{foreign_key} = $f_key;
114 $rel_info->{args} = $args;
116 $class->_extend_meta(
122 quote_sub "${class}::${rel}", sprintf( <<'EOC', perlstring $rel), { '$rf' => \sub { my $o = shift; $o = $o->$_ for @f_method; $o } };
123 my $rs = shift->search_related( %s => @_);
124 $rs->{attrs}{record_filter} = $rf;
125 return (wantarray ? $rs->all : $rs);
134 my ($class, $rel, $f_class, @columns) = @_;
137 if (ref $columns[0] || !defined $columns[0]) {
138 $ret = $class->next::method($rel, $f_class, @columns);
140 $ret = $class->next::method($rel, $f_class, undef,
141 { proxy => \@columns });
144 my $rel_info = $class->result_source_instance->relationship_info($rel);
145 $rel_info->{args}{import} = \@columns;
147 $class->_extend_meta(
157 my ($class, $type, $rel, $val) = @_;
158 my %hash = %{ Clone::clone($class->__meta_info || {}) };
160 $val->{self_class} = $class;
161 $val->{type} = $type;
162 $val->{accessor} = $rel;
164 $hash{$type}{$rel} = DBIx::Class::CDBICompat::Relationship->new($val);
165 $class->__meta_info(\%hash);
170 my ($class, $type, $rel) = @_;
171 my $meta = $class->__meta_info;
172 return $meta unless $type;
174 my $type_meta = $meta->{$type};
175 return $type_meta unless $rel;
176 return $type_meta->{$rel};
183 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
184 $attrs = { %{ pop(@_) } };
186 my $where = (@_ ? ((@_ == 1) ? ((ref $_[0] eq "HASH") ? { %{+shift} } : shift)
189 if (ref $where eq 'HASH') {
190 foreach my $key (keys %$where) { # has_a deflation hack
191 $where->{$key} = ''.$where->{$key}
192 if eval { $where->{$key}->isa('DBIx::Class') };
195 $self->next::method($where, $attrs);
199 return shift->search_related(shift)->new_result(shift);