Skip to content

Instantly share code, notes, and snippets.

@vyspiansky
Last active September 11, 2024 15:57
Show Gist options
  • Save vyspiansky/2e3a0a66087eb68d68de5b60689bc84d to your computer and use it in GitHub Desktop.
Save vyspiansky/2e3a0a66087eb68d68de5b60689bc84d to your computer and use it in GitHub Desktop.
Retrieve a plain SQL query from a Select query object in Drupal 10
<?php
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Query\SelectInterface;
/**
* Returns a plain SQL query from a Select query object.
*/
function get_plain_sql_query(SelectInterface $query): string
{
$sql = $query->__toString();
$args = $query->getArguments();
foreach ($args as $key => $value) {
$sql = str_replace($key, Database::getConnection()->quote($value), $sql);
}
return $sql;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment