我需要找出由某个查询执行的SQL查询。在配置视图时,“视图”模块可以显示SQL,但显然查询不是在所有情况下都可以运行的实际查询。
我知道Devel模块可以显示数据库查询,但是devel无法显示实际查询,除非在每个查询中单击关联的“ A”链接,并且有数百个查询。
我如何找出视图执行的实际查询?该视图显示为块。
我需要找出由某个查询执行的SQL查询。在配置视图时,“视图”模块可以显示SQL,但显然查询不是在所有情况下都可以运行的实际查询。
我知道Devel模块可以显示数据库查询,但是devel无法显示实际查询,除非在每个查询中单击关联的“ A”链接,并且有数百个查询。
我如何找出视图执行的实际查询?该视图显示为块。
Answers:
您必须使用hook_views_pre_execute,并已安装Devel dpq
来查看SQL字符串:
function hook_views_pre_execute(&$view) {
dpq($view->build_info['query']);
}
function hook_views_pre_execute(&$view) {
if ($view->name == 'XYZ') {
$query = (string)$view->build_info['query'];
echo $query;
}
}
无需补丁或挂钩。
// Run the view.
$view = views_get_view('frontpage');
$view->set_display('page');
$view->pre_execute();
$view->execute();
/* Magic Below Here */
// Get query from the view.
$query = $view->query->query();
// Format SelectQueryInterface into a string.
$string = (string) $query;
// Replace arguments.
$arguments = $query->arguments();
if (!empty($arguments) && is_array($arguments)) {
foreach ($arguments as $placeholder => &$value) {
if (is_string($value)) {
$value = "'$value'";
}
}
$string = strtr($string, $arguments);
}
// Format the query string for more readable output.
$string = str_replace(array(' {', "\n{"), ' ', $string);
$string = str_replace(array('} ', "}\n"), ' AS ', $string);
$string = str_replace(', ', ",\n ", $string);
$string = str_replace(' AND ', "\n AND ", $string);
$string = str_replace(' ON ', "\n ON ", $string);
$string = str_replace('SELECT ', "SELECT\n ", $string);
$string = str_replace('ORDER BY ', "ORDER BY\n ", $string);
// echo $string;
echo str_replace(' ', ' ', nl2br($string));
将此作为输出
SELECT
node.sticky AS node_sticky,
node.created AS node_created,
node.nid AS nid,
'frontpage:page' AS view_name
FROM node AS node
WHERE (( (node.promote <> 0)
AND (node.status = 1) ))
ORDER BY
node_sticky DESC,
node_created DESC
LIMIT 10 OFFSET 0
SELECT node.nid AS nid, 'node' AS field_data_field_name_node_entity_type, 'node' AS field_data_field_surname_node_entity_type,
ecc ...
请尝试以下补丁:
--- a/sites/all/modules/views/plugins/views_plugin_query_default.inc
+++ b/sites/all/modules/views/plugins/views_plugin_query_default.inc
@@ -1393,6 +1393,19 @@ class views_plugin_query_default extends views_plugin_query {
$query->range($offset, $limit);
}
+ $query_string = (string)$query;
+ $query_string = str_replace('{', '', $query_string);
+ $query_string = str_replace('}', '', $query_string);
+ $query_params = $query->getArguments();
+ foreach($query_params as $placeholder => $value) {
+ if(!is_numeric($value)) {
+ $query_string = str_replace($placeholder, "'$value'", $query_string);
+ }
+ else {
+ $query_string = str_replace($placeholder, $value, $query_string);
+ }
+ }
+ drupal_set_message($query_string);
$result = $query->execute();
$view->result = array();
//在此处按视图名称和上下文过滤器获取结果
$viewr = views_get_view('top_block_on_product_display');
$args[] = $myarg;
$display_id = 'default';
$viewr->set_arguments($args);
$viewr->set_display($display_id);
$viewr->execute();
$view_content = $viewr->result;
请参阅文档以获取更多参考:https : //api.drupal.org/api/views/views.module/function/views_get_view_result/7.x-3.x