From 83a78eea7bb24102f0b255bd350b7dd343e7a8e4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Fri, 29 May 2015 16:36:46 +0200 Subject: [PATCH] ConventionManager: foreign_key Benennung MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Der Standard Conventionmanager von Rose versucht irgendwelche singular/plural Magic um aus dem key einen guten Namen zu kriegen. Das klappt antürlich mal wieder nicht. Wir haben aber einen besseren Mechanismus dafür, SL::DB::Helper::Mappings. Also werden jetzt die dafür benutzt. --- SL/DB/Helper/ConventionManager.pm | 49 +++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/SL/DB/Helper/ConventionManager.pm b/SL/DB/Helper/ConventionManager.pm index 35c6b1ef2..830d34cda 100644 --- a/SL/DB/Helper/ConventionManager.pm +++ b/SL/DB/Helper/ConventionManager.pm @@ -26,4 +26,53 @@ sub auto_manager_base_class { return 'SL::DB::Helper::Manager'; } +sub auto_foreign_key_name +{ + my($self, $f_class, $current_name, $key_columns, $used_names) = @_; + + my $f_meta = $f_class->meta or return $current_name; + my $package_name = SL::DB::Helper::Mappings::get_name_for_table($f_meta->table); + my $name = $package_name || $current_name; + + if(keys %$key_columns == 1) + { + my($local_column, $foreign_column) = %$key_columns; + + # Try to lop off foreign column name. Example: + # my_foreign_object_id -> my_foreign_object + if($local_column =~ s/_$foreign_column$//i) + { + $name = $local_column; + } + else + { + $name = $package_name || $current_name; + } + } + + # Avoid method name conflicts + if($self->method_name_conflicts($name) || $used_names->{$name}) + { + foreach my $s ('_obj', '_object') + { + # Try the name with a suffix appended + unless($self->method_name_conflicts($name . $s) || + $used_names->{$name . $s}) + { + return $name . $s; + } + } + + my $i = 1; + + # Give up and go with numbers... + $i++ while($self->method_name_conflicts($name . $i) || + $used_names->{$name . $i}); + + return $name . $i; + } + + return $name; +} + 1; -- 2.20.1