Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save tobyontour/e6f6928d3f12b57b03a40d9042f434d9 to your computer and use it in GitHub Desktop.

Select an option

Save tobyontour/e6f6928d3f12b57b03a40d9042f434d9 to your computer and use it in GitHub Desktop.
Get the SQL from a Drupal query with parameters embedded
function logDrupalDatabaseQuery($query) {
// Log the query string.
$tmp = $query->__toString();
// Reverse array to avoid a placeholder ending in .._1 replacing placeholders ending in .._1n
foreach (array_reverse($query->getArguments()) as $key => $value) {
if (is_string($value)) {
$value = '"' . $value . '"';
}
$tmp = str_replace($key, $value, $tmp);
}
// A little bit of formatting.
$tmp = str_replace("AND", "\n AND", $tmp);
// Log the calling function too.
$calling_function = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,3)[2]['function'];
\Drupal::logger(__FUNCTION__)->debug("$calling_function() <pre>$tmp</pre>");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment