Drupal 7在视图中改变查询

时间:2012-02-17 12:22:58

标签: drupal view alter

我正在尝试使用mymodule_views_pre_execute在视图中更改查询,并使用devel查找当前正在使用的sql查询,如下所示:

SELECT node.nid AS nid FROM node node LEFT JOIN field_data_field_date
 field_data_field_date ON node.nid = field_data_field_date.entity_id AND
 (field_data_field_date.entity_type = :views_join_condition_0 AND
 field_data_field_date.deleted     = :views_join_condition_1) 
 WHERE ((
 (DATE_FORMAT(field_data_field_date.field_date_value, '%Y-%m-%d\T%H:%i') > :node_date_filter) )AND
(( (node.status = :db_condition_placeholder_2) )))     
LIMIT 10 OFFSET 0

然后我使用以下内容重新执行此操作:

$query = db_select("node", "n");
            $query->addField("n", "nid");
            $query->leftJoin("{field_data_field_date}", "{field_data_field_date}",
            "n.nid = field_data_field_date.entity_id AND field_data_field_date.entity_type = 'node'
            AND field_data_field_date.deleted = '0'");

            $query->where("(DATE_FORMAT(field_data_field_date.field_date_value, '%Y-%m-%d\T%H:%i') > NOW())");
            $query->where("n.status = '1'");

我必须将views_join_condition_0替换为“node”,将views_join_condition_1替换为“0”并将node_date_filter替换为NOW()虽然我不确定这是不是正确的方法?如果我离开:views_join_condition_0,:views_join_condition_1和:node_date_filter虽然不起作用?!

1 个答案:

答案 0 :(得分:0)

改为使用hook_view_query_alter(&$view, &$query)