drupal函数:hook_db_rewrite_sql

modules/system/system.api.php, 63行

版本
4.6 – 7
hook_db_rewrite_sql($query, $primary_table, $primary_field, $args)

重写数据库查询,通常为访问控制。

Add JOIN and WHERE statements to queries and decide whether the primary_field shall be made DISTINCT. For node objects, primary field is always called nid. For taxonomy terms, it is tid and for vocabularies it is vid. For comments, it is cid. Primary table is the table where the primary object (node, file, taxonomy_term_node etc.) is.

You shall return an associative array. Possible keys are 'join', 'where' and 'distinct'. The value of 'distinct' shall be 1 if you want that the primary_field made DISTINCT.

参数

$query Query to be rewritten.

$primary_table Name or alias of the table which has the primary key field for this query. Typical table names would be: {block}, {comment}, {forum}, {node}, {menu}, {taxonomy_term_data} or {taxonomy_vocabulary}. However, it is more common for $primary_table to contain the usual table alias: b, c, f, n, m, t or v.

$primary_field Name of the primary field.

$args Array of additional arguments.

返回值

An array of join statements, where statements, distinct decision.

相关主题

Hooks
Allow modules to interact with the Drupal core.

代码

<?php function hook_db_rewrite_sql($query, $primary_table, $primary_field, $args) { switch ($primary_field) { case 'nid': // this query deals with node objects $return = array(); if ($primary_table != 'n') { $return['join'] = "LEFT JOIN {node} n ON $primary_table.nid = n.nid"; } $return['where'] = 'created >' . mktime(0, 0, 0, 1, 1, 2005); return $return; break; case 'tid': // this query deals with taxonomy objects break; case 'vid': // this query deals with vocabulary objects break; } } ?>


Syndicate content