1 package # hide from PAUSE
2 DBIx::Class::CDBICompat::Relationships;
6 use base 'Class::Data::Inheritable';
9 use DBIx::Class::CDBICompat::Relationship;
10 use Scalar::Util 'blessed';
11 use DBIx::Class::_Util qw(quote_sub perlstring);
13 __PACKAGE__->mk_classdata('__meta_info' => {});
18 DBIx::Class::CDBICompat::Relationships - Emulate has_a(), has_many(), might_have() and meta_info()
22 Emulate C<has_a>, C<has_many>, C<might_have> and C<meta_info>.
27 my($self, $col, @rest) = @_;
29 $self->_declare_has_a($col, @rest);
30 $self->_mk_inflated_column_accessor($col);
37 my ($self, $col, $f_class, %args) = @_;
38 $self->throw_exception( "No such column ${col}" )
39 unless $self->has_column($col);
40 $self->ensure_class_loaded($f_class);
44 # Class::DBI allows Non database has_a with implicit deflate and inflate
45 # Hopefully the following will catch Non-database tables.
46 if( !$f_class->isa('DBIx::Class::Row') and !$f_class->isa('Class::DBI::Row') ) {
47 $args{'inflate'} ||= sub { $f_class->new(shift) }; # implicit inflate by calling new
48 $args{'deflate'} ||= sub { shift() . '' }; # implicit deflate by stringification
51 if ($args{'inflate'} || $args{'deflate'}) { # Non-database has_a
52 if (!ref $args{'inflate'}) {
53 my $meth = $args{'inflate'};
54 $args{'inflate'} = sub { $f_class->$meth(shift); };
56 if (!ref $args{'deflate'}) {
57 my $meth = $args{'deflate'};
58 $args{'deflate'} = sub { shift->$meth; };
60 $self->inflate_column($col, \%args);
67 $self->belongs_to($col, $f_class);
68 $rel_info = $self->result_source_instance->relationship_info($col);
71 $rel_info->{args} = \%args;
81 sub _mk_inflated_column_accessor {
82 my($class, $col) = @_;
84 return $class->mk_group_accessors('inflated_column' => $col);
88 my ($class, $rel, $f_class, $f_key, $args) = @_;
92 if (ref $f_class eq 'ARRAY') {
93 ($f_class, @f_method) = @$f_class;
96 if (ref $f_key eq 'HASH' && !$args) { $args = $f_key; undef $f_key; };
99 my $cascade = delete $args->{cascade} || '';
100 if (delete $args->{no_cascade_delete} || $cascade eq 'None') {
101 $args->{cascade_delete} = 0;
103 elsif( $cascade eq 'Delete' ) {
104 $args->{cascade_delete} = 1;
106 elsif( length $cascade ) {
107 warn "Unemulated cascade option '$cascade' in $class->has_many($rel => $f_class)";
110 if( !$f_key and !@f_method ) {
111 $class->ensure_class_loaded($f_class);
112 my $f_source = $f_class->result_source_instance;
113 ($f_key) = grep { $f_source->relationship_info($_)->{class} eq $class }
114 $f_source->relationships;
117 $class->next::method($rel, $f_class, $f_key, $args);
119 my $rel_info = $class->result_source_instance->relationship_info($rel);
120 $args->{mapping} = \@f_method;
121 $args->{foreign_key} = $f_key;
122 $rel_info->{args} = $args;
124 $class->_extend_meta(
130 quote_sub "${class}::${rel}", sprintf( <<'EOC', perlstring $rel), { '$rf' => \sub { my $o = shift; $o = $o->$_ for @f_method; $o } };
131 my $rs = shift->search_related( %s => @_);
132 $rs->{attrs}{record_filter} = $rf;
133 return (wantarray ? $rs->all : $rs);
142 my ($class, $rel, $f_class, @columns) = @_;
145 if (ref $columns[0] || !defined $columns[0]) {
146 $ret = $class->next::method($rel, $f_class, @columns);
148 $ret = $class->next::method($rel, $f_class, undef,
149 { proxy => \@columns });
152 my $rel_info = $class->result_source_instance->relationship_info($rel);
153 $rel_info->{args}{import} = \@columns;
155 $class->_extend_meta(
165 my ($class, $type, $rel, $val) = @_;
167 ### Explicitly not using the deep cloner as Clone exhibits specific behavior
168 ### wrt CODE references - it simply passes them as-is to the new structure
169 ### (without deparse/eval cycles). There likely is code that relies on this
170 ### so we just let sleeping dogs lie.
171 my $hash = Clone::clone($class->__meta_info || {});
173 $val->{self_class} = $class;
174 $val->{type} = $type;
175 $val->{accessor} = $rel;
177 $hash->{$type}{$rel} = DBIx::Class::CDBICompat::Relationship->new($val);
178 $class->__meta_info($hash);
183 my ($class, $type, $rel) = @_;
184 my $meta = $class->__meta_info;
185 return $meta unless $type;
187 my $type_meta = $meta->{$type};
188 return $type_meta unless $rel;
189 return $type_meta->{$rel};
196 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
197 $attrs = { %{ pop(@_) } };
199 my $where = (@_ ? ((@_ == 1) ? ((ref $_[0] eq "HASH") ? { %{+shift} } : shift)
202 if (ref $where eq 'HASH') {
203 foreach my $key (keys %$where) { # has_a deflation hack
204 $where->{$key} = ''.$where->{$key} if (
205 defined blessed $where->{$key}
207 $where->{$key}->isa('DBIx::Class')
211 $self->next::method($where, $attrs);
215 return shift->search_related(shift)->new_result(shift);
218 =head1 FURTHER QUESTIONS?
220 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
222 =head1 COPYRIGHT AND LICENSE
224 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
225 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
226 redistribute it and/or modify it under the same terms as the
227 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.