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
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;
dpm($query_string);
-
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.– EmmySJul 5, 2012 at 14:34