php 如何显示 Drupal 视图执行的查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2338213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:06:01  来源:igfitidea点击:

How to display the query executed by the Drupal view

phpdrupaldrupal-6drupal-views

提问by Mech Software

I want to display the query that is executed in the drupal view. Currently in the view editor it shows the query however I have a need to use that query in my code to download an excel version of the view.

我想显示在 drupal 视图中执行的查询。目前在视图编辑器中它显示了查询,但是我需要在我的代码中使用该查询来下载视图的 excel 版本。

Is there a way to get the executed query the same way it's shown in the "editor" window of the views menu? I want this at the time the the view is shown.

有没有办法以与视图菜单的“编辑器”窗口中显示的方式相同的方式获取已执行的查询?在显示视图时我想要这个。

What I plan to do here is to capture the query in the footer, and have that query posted to a process which will send back an XLS resultset. So i'd like the exact query the view is using to display the results.

我计划在这里做的是在页脚中捕获查询,并将该查询发布到一个进程,该进程将发回 XLS 结果集。所以我想要视图用来显示结果的确切查询。

采纳答案by Owen

The query exists in the view object. Depending on where you want to use it, you may want to add the variable in a views preprocess function, or the location you're calling the view (if calling it programatically).

查询存在于视图对象中。根据您想要使用它的位置,您可能希望在视图预处理函数中添加变量,或者您正在调用视图的位置(如果以编程方式调用它)。

If you're just using the default template for it though, you can have access to it there:

如果您只是使用默认模板,则可以在那里访问它:

// ex. somewhere in your views-view--VIEW_NAME.tpl.php
<?php print db_prefix_tables($view->build_info['query']); ?>

Be careful if your process takes arbitrary SQL though, may be better to call it with the view name, and have it programatically pick up results as required. Or, have a secondary display on your view which returns the result in a XLS result set directly.

但是,如果您的进程采用任意 SQL,请小心,最好使用视图名称调用它,并让它根据需要以编程方式获取结果。或者,在您的视图上有一个辅助显示,它直接在 XLS 结果集中返回结果。

回答by Felix Eve

Or you can use hook_views_pre_executealong with devel's dpq function:

或者您可以hook_views_pre_execute与 devel 的 dpq 功能一起使用:

function MY_MODULE_views_pre_execute(&$view) {
  dpq($view->build_info['query']);
}

回答by smokris

Set the Footer's Input Format to PHP, and paste this snippet into the Footer text:

将页脚的输入格式设置为 PHP,并将此代码段粘贴到页脚文本中:

<pre><?php
  $v = views_get_current_view();
  $query = db_prefix_tables(vsprintf($v->build_info['query'], $v->build_info['query_args']));
  $replacements = module_invoke_all('views_query_substitutions', $v);
  $query = str_replace(array_keys($replacements), $replacements, $query);
  echo $query;
?></pre>

For a syntax-highlighted query (using geshifilter.module), use the following snippet:

对于语法突出显示的查询(使用geshifilter.module),请使用以下代码段:

<pre><?php
  require_once drupal_get_path('module', 'geshifilter') .'/geshifilter.pages.inc';
  $v = views_get_current_view();
  $query = db_prefix_tables(vsprintf($v->build_info['query'], $v->build_info['query_args']));
  $replacements = module_invoke_all('views_query_substitutions', $v);
  $query = str_replace(array_keys($replacements), $replacements, $query);
  echo geshifilter_process($query, 'sql');
?></pre>

(Stemmed from @Owen's answer and discussion with @Mech-Software in the comments.)

(源于@Owen 的回答以及评论中与@Mech-Software 的讨论。)

回答by googletorp

The develmodule can log queries for you.

devel的模块可以登录查询你。

回答by Nikit

http://drupal.org/project/views_bonuswill help to export from Views.

http://drupal.org/project/views_bonus将有助于从视图导出。

回答by arcsum

as a side note, for the excel export, Have you tried Views Data Export Module?

作为旁注,对于 excel 导出,您是否尝试过视图数据导出模块?

回答by mcaleaa

How to output view queries to the screen.

如何将视图查询输出到屏幕。

This works for me in Drupal 7.

这在 Drupal 7 中对我有用。

go to:

去:

views/plugins/views_plugin_query_default.inc

视图/插件/views_plugin_query_default.inc

find this function:

找到这个函数:

/**
 * Generate a query and a countquery from all of the information supplied
 * to the object.
 *
 * @param $get_count
 *   Provide a countquery if this is true, otherwise provide a normal query.
 */
function query($get_count = FALSE) {

just before the very end of the function, use dpq($query);

就在函数的最后,使用 dpq($query);

  // Add all query substitutions as metadata.
  $query->addMetaData('views_substitutions', module_invoke_all('views_query_substitutions', $this));
dpq($query);
    return $query;
  }