如何在钩子视图中使用Mysql的函数在drupal 7中查询alter

时间:2013-06-04 07:20:37

标签: drupal drupal-views drupal-hooks

我正在使用views_query_alter进行视图的更改查询。在我的查询中,我想要条件 DATE_FORMAT(FROM_UNIXTIME(field_data_field_hiddeneventdate.field_hiddeneventdate_value), '%m') = '06'

但是当我在尝试时

    $condition = array('field' =>   "DATE_FORMAT(FROM_UNIXTIME(field_data_field_hiddeneventdate.field_hiddeneventdate_value),   '%m')",
'value' => date('%Y-%m'), //$view->exposed_raw_input['title'],
'operator' => '>=',
);

它生成查询:

SELECT node.title AS node_title, node.nid AS nid, field_data_field_hiddeneventdate.field_hiddeneventdate_value AS node_changed
FROM 
{node} node
LEFT JOIN {field_data_field_hiddeneventdate} field_data_field_hiddeneventdate ON node.nid = field_data_field_hiddeneventdate.entity_id AND (field_data_field_hiddeneventdate.entity_type = node AND field_data_field_hiddeneventdate.deleted = 0)
WHERE (( (node.status = '1') AND (node.type IN  ('events_meeting')) )AND( (DATE_FORMATFROM_UNIXTIMEfield_data_field_hiddeneventdate.field_hiddeneventdate_valuem >= '%2013-%06') ))
LIMIT 10 OFFSET 0.

但我想:

SELECT node.title AS node_title, node.nid AS nid, field_data_field_hiddeneventdate.field_hiddeneventdate_value AS node_changed,
DATE_FORMAT(FROM_UNIXTIME(field_data_field_hiddeneventdate.field_hiddeneventdate_value), '%d-%m-%Y') AS dt
FROM node node
LEFT JOIN field_data_field_hiddeneventdate field_data_field_hiddeneventdate 
ON 
node.nid = field_data_field_hiddeneventdate.entity_id 
AND 
(field_data_field_hiddeneventdate.entity_type = 'node' 
AND field_data_field_hiddeneventdate.deleted = 0)
WHERE (( (node.status = '1') AND (node.type IN  ('events_meeting')) )
AND(           DATE_FORMAT(FROM_UNIXTIME(field_data_field_hiddeneventdate.field_hiddeneventdate_value), '%m') = '06' 
    )
)
LIMIT 10 OFFSET 0;

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

如果要更改视图的查询,为什么不使用视图并执行db_select。直接数据库查询比Drupal中的视图更快。