1 package DBIx::Class::CDBICompat::HasMany;
7 my ($class, $rel, $f_class, $f_key, $args) = @_;
8 #die "No such column ${col}" unless $class->_columns->{$col};
9 eval "require $f_class";
10 my ($pri, $too_many) = keys %{ $class->_primaries };
11 die "has_many only works with a single primary key; ${class} has more"
13 if (ref $f_key eq 'HASH') { $args = $f_key; undef $f_key; };
15 ($f_key) = grep { $_->{class} && $_->{class} eq $class }
16 $f_class->_relationships;
19 #warn join(', ', %{ $f_class->_columns });
20 $class =~ /([^\:]+)$/;
22 $f_key = lc $1 if $f_class->_columns->{lc $1};
24 die "Unable to resolve foreign key for has_many from ${class} to ${f_class}"
26 die "No such column ${f_key} on foreign class ${f_class}"
27 unless $f_class->_columns->{$f_key};
28 $class->add_relationship($rel, $f_class,
29 { "foreign.${f_key}" => "self.${pri}" },
30 { _type => 'has_many', %{$args || {}} } );
33 *{"${class}::${rel}"} = sub { shift->search_related($rel, @_); };
34 *{"${class}::add_to_${rel}"} = sub { shift->create_related($rel, @_); };
40 my ($self, @rest) = @_;
41 return $self->NEXT::ACTUAL::delete(@rest) unless ref $self;
42 # I'm just ignoring this for class deletes because hell, the db should
43 # be handling this anyway. Assuming we have joins we probably actually
44 # *could* do them, but I'd rather not.
46 my $ret = $self->NEXT::ACTUAL::delete(@rest);
48 my %rels = %{ $self->_relationships };
49 my @hm = grep { $rels{$_}{attrs}{_type}
50 && $rels{$_}{attrs}{_type} eq 'has_many' } keys %rels;
51 foreach my $has_many (@hm) {
52 $_->delete for $self->search_related($has_many);