Created
May 29, 2025 13:53
-
-
Save tobyontour/e6f6928d3f12b57b03a40d9042f434d9 to your computer and use it in GitHub Desktop.
Get the SQL from a Drupal query with parameters embedded
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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