I'm using hook_views_query_alter(), and I'd really like to print somewhere (screen, error log, whatever) the query that's being run based on the alterations. I've tried dpm($query), but that gives me the whole krumo tree - all I want is the query - i.e. "select x, y from z where a = 2". Possible?


1 Answer 1


The views_plugin_query_default class has a query() method that will take the parameters currently held in the object and provide you with a standard SelectQuery (un-executed of course).

Since the SelectQuery class implements the magic __toString() method you can then cast it directly to a string, which will be the SQL query itself:

$select_query = $query->query();
$query_string = (string)$select_query;
  • Thanks very much. I got an error: "Notice: Trying to get property of non-object in views_plugin_query_default->query() (line 1217 of /home/www/expo/sites/all/modules/contrib/views/plugins/views_plugin_query_default.inc)." But it did show the query, which is all I care about.
    – EmmyS
    Commented Jul 5, 2012 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.