How to print query in Drupal 8?

Member

aniya.jaskolski

by aniya.jaskolski, in category: PHP Frameworks , 4 months ago

How to print query in Drupal 8?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by dmitrypro77 , 3 months ago

@aniya.jaskolski you can cast to string or use toString() method to print raw SQL query in Drupal 8, code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php

$connection = \Drupal::service('database');
$query = $connection->select('node', 'node');
$query->fields('node', ['nid']);
$query->condition('node.type', 'article');

// Print query
print_r($query->__toString());
// Print query params
print_r($query->arguments());