X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSchema%2FLoader%2FRelBuilder.pm;h=76f2139c52e53730c0e8d78703722357296d1d54;hb=188038c0d8bf2df07b728e070bee4ca5d2de3026;hp=e8d112834bbdfb2b486305ed2e5ee3d5aae5ae3c;hpb=dab9a47a1ff7f40d68d72bc9c5ab2b3fdd87f878;p=dbsrgits%2FDBIx-Class-Schema-Loader.git diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm index e8d1128..76f2139 100644 --- a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm @@ -6,10 +6,9 @@ use base 'Class::Accessor::Grouped'; use mro 'c3'; use Carp::Clan qw/^DBIx::Class/; use Scalar::Util 'weaken'; -use DBIx::Class::Schema::Loader::Utils qw/split_name slurp_file array_eq/; +use DBIx::Class::Schema::Loader::Utils qw/split_name slurp_file array_eq apply uniq/; use Try::Tiny; -use List::Util 'first'; -use List::MoreUtils qw/apply uniq any/; +use List::Util qw/all any first/; use namespace::clean; use Lingua::EN::Inflect::Phrase (); use Lingua::EN::Tagger (); @@ -18,7 +17,7 @@ use String::ToIdentifier::EN::Unicode (); use Class::Unload (); use Class::Inspector (); -our $VERSION = '0.07040'; +our $VERSION = '0.07048'; # Glossary: # @@ -95,16 +94,18 @@ This method will return the generated relationships as a hashref keyed on the class names. The values are arrayrefs of hashes containing method name and arguments, like so: - { - 'Some::Source::Class' => [ - { method => 'belongs_to', arguments => [ 'col1', 'Another::Source::Class' ], - { method => 'has_many', arguments => [ 'anothers', 'Yet::Another::Source::Class', 'col15' ], - ], - 'Another::Source::Class' => [ - # ... - ], - # ... - } + { + 'Some::Source::Class' => [ + { method => 'belongs_to', arguments => [ + 'col1', 'Another::Source::Class' ] }, + { method => 'has_many', arguments => [ + 'anothers', 'Yet::Another::Source::Class', 'col15' ] }, + ], + 'Another::Source::Class' => [ + # ... + ], + # ... + } =cut @@ -116,6 +117,7 @@ __PACKAGE__->mk_group_accessors('simple', qw/ relationship_attrs rel_collision_map rel_name_map + allow_extra_m2m_cols _temp_classes __tagger /); @@ -134,12 +136,11 @@ sub new { my $self = { loader => $loader, - schema => $loader->schema, - inflect_plural => $loader->inflect_plural, - inflect_singular => $loader->inflect_singular, - relationship_attrs => $loader->relationship_attrs, - rel_collision_map => $loader->rel_collision_map, - rel_name_map => $loader->rel_name_map, + (map { $_ => $loader->$_ } qw( + schema inflect_plural inflect_singular + relationship_attrs rel_collision_map + rel_name_map allow_extra_m2m_cols + )), _temp_classes => [], }; @@ -526,8 +527,14 @@ sub _generate_m2ms { $class{class} = $rels[$this]{args}[1]; + my %link_cols = map { $_ => 1 } apply { s/^self\.//i } values %{ $rels[$this]{args}[2] }; + $class{link_table_rel} = first { - $_->{method} eq 'has_many' && $_->{args}[1] eq $link_class + $_->{method} eq 'has_many' + and + $_->{args}[1] eq $link_class + and + all { $link_cols{$_} } apply { s/^foreign\.//i } keys %{$_->{args}[2]} } @{ $all_code->{$class{class}} }; next LINK_CLASS unless $class{link_table_rel}; @@ -541,6 +548,8 @@ sub _generate_m2ms { } ]; $class{to_cols} = [ apply { s/^foreign\.//i } keys %{ $rels[$that]{args}[2] } ]; + + $class{from_link_cols} = [ apply { s/^self\.//i } values %{ $rels[$this]{args}[2] } ]; } my $link_moniker = $rels[0]{extra}{local_moniker}; @@ -551,8 +560,10 @@ sub _generate_m2ms { my @link_table_primary_cols = @{[ $self->schema->source($link_moniker)->primary_columns ]}; - next unless @{$class[0]{to_cols}} + @{$class[1]{to_cols}} == @link_table_cols - && @link_table_cols == @link_table_primary_cols; + next unless array_eq( + [ sort +uniq @{$class[0]{from_link_cols}}, @{$class[1]{from_link_cols}} ], + [ sort @link_table_primary_cols ], + ) && ($self->allow_extra_m2m_cols || @link_table_cols == @link_table_primary_cols); foreach my $this (0, 1) { my $that = $this ? 0 : 1; @@ -958,9 +969,9 @@ sub _cleanup { $self->_temp_classes([]); } -=head1 AUTHOR +=head1 AUTHORS -See L and L. +See L. =head1 LICENSE