title: Add Table Join, Where, and Order By to Views Query in views_query_alter()
body: <p>I am trying to modify the query for Views in Drupal (Views version 3, Drupal version 7).</p>
  
  <p>What I want to do is change the query prior to running such that it LEFT JOINs a table in which I have weights assigned to the nodes.</p>
  
  <p>If I was to write the query I want in SQL, it would look like this:</p>
  
  <pre><code>    SELECT a.nid, a.title, a.description
      FROM node a
      LEFT OUTER JOIN node_weights b
      ON a.nid = b.nid
      WHERE b.uid = $uid
      ORDER BY b.weight DESC
  </code></pre>
  
  <p>This query works like a champ when I run it in the query analyzer. So, now I need to make it work in my module.</p>
  
  <p>I've seen multiple approaches detailed on various blogs for different ways to modify View queries, but they seem to be addressing different versions of Views. So it is very confusing to try to determine whether anything I'm looking at could even possibly work for my application.</p>
  
  <p>It seems that I need to use a MODULE_NAME_views_tables() function to tell Views what the relationship is between the table I want to join and the node table.</p>
  
  <p>I've added the following functions to MODULE_NAME.views.inc:</p>
  
  <pre><code>    function MODULE_NAME_views_tables() {
        $tables['node_weights'] = array(
          "name" =&gt; "node_weights",
          "join" =&gt; array(
            "left" =&gt; array(
              "table" =&gt; "node",
              "field" =&gt; "nid"
            ),
            "right" =&gt; array(
              "field" =&gt; "nid"
            ),
          ),
        );
        return $table;  
      }
  </code></pre>
  
  <p>This does seem to be working because when I use Krumo to look at the query array, I see my "node_weights" table in the "table_queue" element.</p>
  
  <p>In the views_query_alter() function, I'd like it to work something like this:</p>
  
  <pre><code>    function MODULE_NAME_views_query_alter(&amp;$view, &amp;$query) {
        $uid = $_COOKIE['uid']; 
        $view-&gt;query-&gt;add_relationship('node_weights', new views_join('node_weights', 'nid', 'node', 'nid','LEFT'));
        $view-&gt;query-&gt;add_where('node_weights', "node_weights.uid", $uid);
        krumo($query);
      }
  </code></pre>
  
  <p>This function barfs pretty badly. Although my join table is appearing in the $view object, the add_relationship method is throwing an error for a 3rd argument, but I don't see any examples online that have 3 arguments so I don't know what it's missing.</p>
  
  <p>Also, I'm pretty sure my add_where method isn't correct, but I don't know what the inputs should actually be. This is just a blind guess.</p>
  
  <p>The bottom line is that I want to join the node table to my node_weights table, and then make sure my weights are used in the query to sort the results in a descending fashion where the user id = the user id in my table, and the tables are joined on the nid field.</p>
  
  <p>Thanks in advance. </p>