A bit of background... If you are building up a complex join query in a module that uses the node table several times, you have to alias the node table in different ways in different parts of your query, and you then have to be careful about db_rewrite_sql. You end up calling _db_rewrite_sql() several times, and doing some funky things with the return value, in order to get the db rewrite stuff to work correctly.

In particular, your table alias for {node} is not "n" or "node" when you call _db_rewrite_sql. So when you call _db_rewrite_sql, the args are generally $query = '', $primary_table = 'some_alias', $primary_field = 'nid'.

So far, so good. Then _db_rewrite_sql invokes hook_db_rewrite_sql(). Some modules, such as the core Node module, check to see what table is being used by checking the primary field arg to the hook, e.g.:

function node_db_rewrite_sql($query, $primary_table, $primary_field) {
  if ($primary_field == 'nid' && !node_access_view_all_nodes()) {
    $return['join'] = _node_access_join_sql($primary_table);
    $return['where'] = _node_access_where_sql();
    $return['distinct'] = 1;
    return $return;
  }
}

So these modules work fine with table aliases, as long as you pass in the right primary key.

But the internationalization module switches on $primary_table, so that is not working with my module (Search by Page Nodes), and searches are not being restricted to one language as they should.

A small fix (I'll attach a patch) fixes this in i18n.module, and I think doesn't break anything else.

CommentFileSizeAuthor
i18n-rewrite-sql.patch751 bytesjhodgdon
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Jose Reyero’s picture

Component: Code » Blocks
Status: Needs review » Needs work

I don't think this works with other modules, since primary key is nid for a number of different tables.

At this point I would be afraid of breaking any other query. Why don't we fix it for your module by using safer table aliases like node1, node2, etc.. and we can add that exception into i18n?

jhodgdon’s picture

What other tables besides node use 'nid' as the **primary** key? I realize nid is a field in a lot of other tables, but I don't think a lot of them use nid as the primary key.

And they would also need to be tables that get passed into db_rewrite_sql as primary tables, which I think are usually just node and taxonomy (and maybe comment). I don't know of any others that are passed to db_rewrite_sql as primary tables?

joseph.olstad’s picture

Status: Needs work » Closed (outdated)