1 package # hide from PAUSE
2 DBIx::Class::CDBICompat::Relationships;
6 use base 'DBIx::Class';
9 use DBIx::Class::CDBICompat::Relationship;
10 use Scalar::Util 'blessed';
11 use DBIx::Class::_Util qw(quote_sub perlstring);
14 __PACKAGE__->mk_classdata('__meta_info' => {});
19 DBIx::Class::CDBICompat::Relationships - Emulate has_a(), has_many(), might_have() and meta_info()
23 Emulate C<has_a>, C<has_many>, C<might_have> and C<meta_info>.
28 my($self, $col, @rest) = @_;
30 $self->_declare_has_a($col, @rest);
31 $self->_mk_inflated_column_accessor($col);
38 my ($self, $col, $f_class, %args) = @_;
39 $self->throw_exception( "No such column ${col}" )
40 unless $self->has_column($col);
41 $self->ensure_class_loaded($f_class);
45 # Class::DBI allows Non database has_a with implicit deflate and inflate
46 # Hopefully the following will catch Non-database tables.
47 if( !$f_class->isa('DBIx::Class::Row') and !$f_class->isa('Class::DBI::Row') ) {
48 $args{'inflate'} ||= sub { $f_class->new(shift) }; # implicit inflate by calling new
49 $args{'deflate'} ||= sub { shift() . '' }; # implicit deflate by stringification
52 if ($args{'inflate'} || $args{'deflate'}) { # Non-database has_a
53 if (!ref $args{'inflate'}) {
54 my $meth = $args{'inflate'};
55 $args{'inflate'} = sub { $f_class->$meth(shift); };
57 if (!ref $args{'deflate'}) {
58 my $meth = $args{'deflate'};
59 $args{'deflate'} = sub { shift->$meth; };
61 $self->inflate_column($col, \%args);
68 $self->belongs_to($col, $f_class);
69 $rel_info = $self->result_source->relationship_info($col);
72 $rel_info->{args} = \%args;
82 sub _mk_inflated_column_accessor {
83 my($class, $col) = @_;
85 return $class->mk_group_accessors('inflated_column' => $col);
89 my ($class, $rel, $f_class, $f_key, $args) = @_;
93 if (ref $f_class eq 'ARRAY') {
94 ($f_class, @f_method) = @$f_class;
97 if (ref $f_key eq 'HASH' && !$args) { $args = $f_key; undef $f_key; };
100 my $cascade = delete $args->{cascade} || '';
101 if (delete $args->{no_cascade_delete} || $cascade eq 'None') {
102 $args->{cascade_delete} = 0;
104 elsif( $cascade eq 'Delete' ) {
105 $args->{cascade_delete} = 1;
107 elsif( length $cascade ) {
108 warn "Unemulated cascade option '$cascade' in $class->has_many($rel => $f_class)";
111 if( !$f_key and !@f_method ) {
112 $class->ensure_class_loaded($f_class);
113 my $f_source = $f_class->result_source;
114 ($f_key) = grep { $f_source->relationship_info($_)->{class} eq $class }
115 $f_source->relationships;
118 $class->next::method($rel, $f_class, $f_key, $args);
120 my $rel_info = $class->result_source->relationship_info($rel);
121 $args->{mapping} = \@f_method;
122 $args->{foreign_key} = $f_key;
123 $rel_info->{args} = $args;
125 $class->_extend_meta(
132 { '$rf' => \sub { my $o = shift; $o = $o->$_ for @f_method; $o } },
133 { attributes => [ 'DBIC_method_is_generated_from_resultsource_metadata' ] },
136 quote_sub "${class}::${rel}", sprintf( <<'EOC', perlstring $rel), @qsub_args;
137 my $rs = shift->related_resultset(%s)->search_rs( @_);
138 $rs->{attrs}{record_filter} = $rf;
139 return (wantarray ? $rs->all : $rs);
148 my ($class, $rel, $f_class, @columns) = @_;
151 if (ref $columns[0] || !defined $columns[0]) {
152 $ret = $class->next::method($rel, $f_class, @columns);
154 $ret = $class->next::method($rel, $f_class, undef,
155 { proxy => \@columns });
158 my $rel_info = $class->result_source->relationship_info($rel);
159 $rel_info->{args}{import} = \@columns;
161 $class->_extend_meta(
171 my ($class, $type, $rel, $val) = @_;
173 ### Explicitly not using the deep cloner as Clone exhibits specific behavior
174 ### wrt CODE references - it simply passes them as-is to the new structure
175 ### (without deparse/eval cycles). There likely is code that relies on this
176 ### so we just let sleeping dogs lie.
177 my $hash = Clone::clone($class->__meta_info || {});
179 $val->{self_class} = $class;
180 $val->{type} = $type;
181 $val->{accessor} = $rel;
183 $hash->{$type}{$rel} = DBIx::Class::CDBICompat::Relationship->new($val);
184 $class->__meta_info($hash);
189 my ($class, $type, $rel) = @_;
190 my $meta = $class->__meta_info;
191 return $meta unless $type;
193 my $type_meta = $meta->{$type};
194 return $type_meta unless $rel;
195 return $type_meta->{$rel};
202 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
203 $attrs = { %{ pop(@_) } };
205 my $where = (@_ ? ((@_ == 1) ? ((ref $_[0] eq "HASH") ? { %{+shift} } : shift)
208 if (ref $where eq 'HASH') {
209 foreach my $key (keys %$where) { # has_a deflation hack
210 $where->{$key} = ''.$where->{$key} if (
211 defined blessed $where->{$key}
213 $where->{$key}->isa('DBIx::Class')
217 $self->next::method($where, $attrs);
221 return shift->search_related(shift)->new_result(@_);
224 =head1 FURTHER QUESTIONS?
226 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
228 =head1 COPYRIGHT AND LICENSE
230 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
231 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
232 redistribute it and/or modify it under the same terms as the
233 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.