【问题标题】:How to display the query executed by the Drupal view如何显示 Drupal 视图执行的查询
【发布时间】:2010-02-25 22:39:15
【问题描述】:

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

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

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

【问题讨论】:

    标签: php drupal drupal-6 drupal-views


    【解决方案1】:

    或者你可以使用hook_views_pre_execute和devel的dpq函数:

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

    【讨论】:

      【解决方案2】:

      将Footer的输入格式设置为PHP,并将这个sn-p粘贴到Footer文本中:

      <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>
      

      对于语法高亮查询(使用geshifilter.module),使用以下sn-p:

      <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>
      

      (源于@Owen 在 cmets 中与@Mech-Software 的回答和讨论。)

      【讨论】:

        【解决方案3】:

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

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

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

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

        【讨论】:

        • 这看起来或多或少是我想要的,但我试图将它放入“页脚”与 view.tps.php 中。我尝试添加 globals $view 但它什么也没返回。如果我能让它发挥作用,这看起来很有希望。
        • Woot,我快到了,除了表单数据的替换之外,我已经成功了。少了什么东西? $current_view = views_get_current_view();打印 db_prefix_tables($current_view->build_info['query']);但是,查询返回 [sql clipped] WHERE (node.status 0) AND (node.type IN ('%s')) AND (casetracker_case.case_status_id = '%s') ORDER BY node_comment_statistics_last_updated DESC 怎么办我得到了 %s 替换吗?
        • 通过使用 query_args 并通过 sprintf 运行它来工作。非常感谢,您为我节省了很多工作。
        【解决方案4】:

        devel 模块可以为您记录查询。

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

            作为旁注, 对于excel导出,你试过Views Data Export Module吗?

            【讨论】:

              【解决方案7】:

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

              这在 Drupal 7 中适用于我。

              前往:

              views/plugins/views_plugin_query_default.inc

              找到这个函数:

              /**
               * 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) {
              

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

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-12-05
                相关资源
                最近更新 更多