Further tradeoffs on relationship sanity checking (augments 9e7525a2)
[dbsrgits/DBIx-Class.git] / t / lib / DBICTest / DynamicForeignCols / TestComputer.pm
1 package DBICTest::DynamicForeignCols::TestComputer;
2
3 use warnings;
4 use strict;
5
6 use base 'DBIx::Class::Core';
7
8 __PACKAGE__->table('TestComputer');
9 __PACKAGE__->add_columns(qw( test_id ));
10 __PACKAGE__->_add_join_column({ class => 'DBICTest::DynamicForeignCols::Computer', method => 'computer' });
11 __PACKAGE__->set_primary_key('test_id', 'computer_id');
12 __PACKAGE__->belongs_to(computer => 'DBICTest::DynamicForeignCols::Computer', 'computer_id');
13
14 ###
15 ### This is a pathological case lifted from production. Yes, there is code
16 ### like this in the wild
17 ###
18 sub _add_join_column {
19    my ($self, $params) = @_;
20
21    my $class = $params->{class};
22    my $method = $params->{method};
23
24    $self->ensure_class_loaded($class);
25
26    my @class_columns = $class->primary_columns;
27
28    if (@class_columns = 1) {
29       $self->add_columns( "${method}_id" );
30    } else {
31       my $i = 0;
32       for (@class_columns) {
33          $i++;
34          $self->add_columns( "${method}_${i}_id" );
35       }
36    }
37 }
38
39 1;