Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the query executed by the Drupal view

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.

like image 627
Mech Avatar asked Feb 25 '10 22:02

Mech


People also ask

How do I print a query in Drupal 7?

Usually we use dpm() for printing strings and queries in Drupal 6 and 7. However if we need to execute the query for debugging, dpm() will print only the object which is not that useful. dpq() is used in such a situation where it will print the query string with actual arguments.

Where are views stored in Drupal?

SQL Views are stored as individual objects in the database. Though you can use Drupal's database functions to select data from them, SQL Views are otherwise not known within Drupal unless you integrate them with the Views module or use something like the Data module to expose them to some extent.

How do I view Drupal?

Go to the “Views settings” page under /admin/structure/views/settings and check the “Show the SQL query” option of the “Live preview settings.” Then you can go to any view and upon clicking the “Update preview” button you will see the SQL query issued by Drupal.

How do I run a query in Drupal 8?

As in the seventh version, Drupal 8 builds the query based on methods such as fields (), join (), condition (), and so on. You can execute the query by calling 'the execute `() method. $query = \Drupal::database()->select('node_field_data', 'nfd'); $query->fields('nfd', ['uid', 'title']); $query->condition ('nfd.


1 Answers

Or you can use hook_views_pre_execute along with devel's dpq function:

function MY_MODULE_views_pre_execute(&$view) {
  dpq($view->build_info['query']);
}
like image 75
Felix Eve Avatar answered Oct 20 '22 05:10

Felix Eve