Showing 2 changed files with 27 additions and 0 deletions
+21
lib/DBIx/Custom.pm
... ...
@@ -555,6 +555,23 @@ sub select {
555 555
     my $relation  = $args{relation};
556 556
     my $append    = $args{append};
557 557
     my $filter    = $args{filter};
558
+
559
+    # Relation table
560
+    if (!$selection && $relation) {
561
+        foreach my $rcolumn (keys %$relation) {
562
+            my $table1 = (split (/\./, $rcolumn))[0];
563
+            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
564
+            
565
+            my $table1_exists;
566
+            my $table2_exists;
567
+            foreach my $table (@$tables) {
568
+                $table1_exists = 1 if $table eq $table1;
569
+                $table2_exists = 1 if $table eq $table2;
570
+            }
571
+            push @$tables, $table1 unless $table1_exists;
572
+            push @$tables, $table2 unless $table2_exists;
573
+        }
574
+    }
558 575
     
559 576
     # SQL stack
560 577
     my @sql;
... ...
@@ -610,6 +627,10 @@ sub select {
610 627
     if (!$selection && $relation) {
611 628
         push @sql, $swhere eq '' ? 'where' : 'and';
612 629
         foreach my $rcolumn (keys %$relation) {
630
+            my $table1 = (split (/\./, $rcolumn))[0];
631
+            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
632
+            push @$tables, ($table1, $table2);
633
+            
613 634
             push @sql, ("$rcolumn = " . $relation->{$rcolumn},  'and');
614 635
         }
615 636
     }
+6
t/dbix-custom-core-sqlite.t
... ...
@@ -1258,6 +1258,12 @@ $result = $dbi->select(
1258 1258
 );
1259 1259
 is($result->fetch_first->[0], 'B');
1260 1260
 
1261
+$result = $dbi->select(
1262
+    table => 'company', relation => {'company.location_id' => 'location.id'},
1263
+    column => ['location.name as location__name']
1264
+);
1265
+is($result->fetch_first->[0], 'B');
1266
+
1261 1267
 test 'selection';
1262 1268
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1263 1269
 $dbi->execute($CREATE_TABLE->{0});