? 433840_join_non_base_tables.patch
Index: tw.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/tw/tw.module,v
retrieving revision 1.1.2.25
diff -u -p -r1.1.2.25 tw.module
--- tw.module	14 Apr 2009 21:04:33 -0000	1.1.2.25
+++ tw.module	20 Apr 2009 02:31:32 -0000
@@ -400,9 +400,9 @@ function tw_menu() {
     'access arguments' => array(TW_ACCESS),
     'file' => 'tw_pages.inc',
   );
-  $items['admin/content/tw/relationships'] = array(
-    'title' => 'Relationships',
-    'description' => 'Manage relationships between managed tables',
+  $items['admin/content/tw/relate-tables'] = array(
+    'title' => 'Relate tables',
+    'description' => 'Relate managed tables.',
     'page callback' => 'tw_relationships',
     'access arguments' => array(TW_ACCESS),
     'file' => 'tw_pages.inc',
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:31:32 -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,
       );
     }
   }
Index: tw_pages.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/tw/tw_pages.inc,v
retrieving revision 1.1.2.25
diff -u -p -r1.1.2.25 tw_pages.inc
--- tw_pages.inc	17 Apr 2009 20:15:55 -0000	1.1.2.25
+++ tw_pages.inc	20 Apr 2009 02:31:33 -0000
@@ -668,8 +668,10 @@ function tw_relationships() {
 function _tw_relationships_form($form_state) {
   $form['description'] = array(
     '#prefix' => '<div>',
-    '#value' => t('Define relationships between tables, enabling the creation
-      of views joining those tables.'),
+    '#value' => t('Relate tables, enabling the creation of views joining those 
+      tables. If the right table is a base table Table Wizard will generate a 
+      <em>Views relationship</em>, if the right table is not a base table, Table Wizard
+      will generate a <em>Views join definition</em>.'),
     '#suffix' => '</div>',
   );
   $form['header'] = array(
@@ -712,11 +714,11 @@ function _tw_relationships_form($form_st
   $form['#theme'] = 'tw_relationships';
   $form['delete'] = array(
     '#type' => 'submit',
-    '#value' => t('Delete selected relationships'),
+    '#value' => t('Delete selected related tables'),
   );
   $form['addrel'] = array(
     '#type' => 'fieldset',
-    '#title' => t('Add a relationship'),
+    '#title' => t('Relate tables'),
     '#collapsible' => TRUE,
   );
   $sql = "SELECT twc.twcid, twc.colname, twt.tablename, twt.connection
