? 433840_join_non_base_tables.patch
Index: tw.views.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/tw/tw.views.inc,v
retrieving revision 1.1.2.9
diff -u -p -r1.1.2.9 tw.views.inc
--- tw.views.inc	14 Apr 2009 20:36:59 -0000	1.1.2.9
+++ tw.views.inc	20 Apr 2009 02:14:51 -0000
@@ -84,7 +84,7 @@ function tw_views_data() {
   }
 
   // Now that all tables are present, fill in relationships defined by foreign keys
-  $sql = "SELECT twt1.tablename tbl1, twc1.colname col1, twt2.tablename tbl2, twc2.colname col2
+  $sql = "SELECT twt1.tablename tbl1, twc1.colname col1, twt2.twtid twtid2, twt2.tablename tbl2, twt2.connection con2, twc2.colname col2
           FROM {tw_relationships} twr
           INNER JOIN {tw_columns} twc1 ON twr.leftcol=twc1.twcid
           INNER JOIN {tw_tables} twt1 ON twc1.twtid=twt1.twtid
@@ -99,24 +99,40 @@ function tw_views_data() {
     extract($row);
     $rawtbl1 = schema_unprefix_table($tbl1);
     $rawtbl2 = schema_unprefix_table($tbl2);
-    if (!isset($tables[$rawtbl1][$col1]['relationship'])) {
-      $tables[$rawtbl1][$col1]['title'] = t("$col1 (joins to $tbl2)");
-      $tables[$rawtbl1][$col1]['relationship'] = array(
-        'base' => $rawtbl2,
-        'base field' => $col2,
-        'label' => t("Join $tbl1 to $tbl2"),
-      );
-    } else {
-      $i++;
-      $mungedcol = $col1.'_'.$i;
-      $tables[$rawtbl1][$mungedcol] = $tables[$rawtbl1][$col1];
-      $tables[$rawtbl1][$mungedcol]['title'] = t("$col1 (joins to $tbl2)");
-      $tables[$rawtbl1][$mungedcol]['real field'] = $col1;
-      $tables[$rawtbl1][$mungedcol]['relationship'] = array(
-        'base' => $rawtbl2,
-        'base field' => $col2,
-        'relationship field' => $col1,
-        'label' => t("Join $tbl1 to $tbl2"),
+
+    // Only create relationship if the right table is a base table.
+    $pk_count = db_result(db_query('SELECT COUNT(*) FROM {tw_columns} WHERE twtid = %d AND primarykey = 1', $twtid2));
+    if ($pk_count == 1) {
+      drupal_set_message("Relationship join information for $tbl1 - $tbl2");
+      if (!isset($tables[$rawtbl1][$col1]['relationship'])) {
+        $tables[$rawtbl1][$col1]['title'] = t("$col1 (joins to $tbl2)");
+        $tables[$rawtbl1][$col1]['relationship'] = array(
+          'base' => $rawtbl2,
+          'base field' => $col2,
+          'label' => t("Join $tbl1 to $tbl2"),
+        );
+      } else {
+        $i++;
+        $mungedcol = $col1.'_'.$i;
+        $tables[$rawtbl1][$mungedcol] = $tables[$rawtbl1][$col1];
+        $tables[$rawtbl1][$mungedcol]['title'] = t("$col1 (joins to $tbl2)");
+        $tables[$rawtbl1][$mungedcol]['real field'] = $col1;
+        $tables[$rawtbl1][$mungedcol]['relationship'] = array(
+          'base' => $rawtbl2,
+          'base field' => $col2,
+          'relationship field' => $col1,
+          'label' => t("Join $tbl1 to $tbl2"),
+        );
+      }
+    }
+    else {
+      // If the right table is not a base table, provide simple JOIN 
+      // information. Make sure the table has a group label.
+      $disptablename = $con2 .'.'. $tbl2;
+      $tables[$rawtbl2]['table']['group'] = $disptablename;
+      $tables[$rawtbl2]['table']['join'][$rawtbl1] = array(
+        'left_field' => $col1,
+        'field' => $col2,
       );
     }
   }
