tags:

views:

50

answers:

3

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.

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.

A: 

The devel module can log queries for you.

googletorp
A: 

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

Nikit
+2  A: 

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.

Owen
This looks more or less what I want but I'm trying to put this into the "footer" vs the view.tps.php. I tried adding globals $view but it returned nothing. This looks promising if I can get it working.
Mech Software
Woot, I'm almost there, I got it working EXCEPT for the substituion of the form data. What's missing?$current_view = views_get_current_view();print db_prefix_tables($current_view->build_info['query']);However, the query comes back with [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 DESCHow do I get the %s substitution included?
Mech Software
Got it working by using the query_args and running that through sprintf. Thanks a ton, you saved me a lot of work.
Mech Software
nice! glad to be of help :)
Owen