Skip to content

Instantly share code, notes, and snippets.

@WengerK
Created September 4, 2017 09:46
Show Gist options
  • Save WengerK/605a216b2eb3341735c38cb0bdd9ed7a to your computer and use it in GitHub Desktop.
Save WengerK/605a216b2eb3341735c38cb0bdd9ed7a to your computer and use it in GitHub Desktop.
Drupal 8 - Print raw SQL queries for debugging
<?php
/**
* Debugging using the database connection.
*/
/** @var \Drupal\Core\Database\Connection */
$connection = \Drupal::service('database');
$query = $connection->select('node', 'node');
$query->fields('node', ['nid'])
->condition('node.type', 'page')
// Debug.
dump($query->__toString());
<?php
/**
* Debugging using the query factory.
*/
// Update the Drupal\Core\Entity\Query\Sql\Query & change the property $sqlQuery to be public then...
$queryFactory = \Drupal::service('entity.query');
$query = $queryFactory->get('node');
$query->condition('type', 'page'):
// Debug.
dump($query->execute());
dump($query->sqlQuery->__toString());
@hugronaphor
Copy link

$query->getQueryString()

@harivenuqed
Copy link

Not working for D10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment