How can I fetch the raw sql version of the following statement?

             $t = \Drupal::entityTypeManager()
              ->loadByProperties(['name' => $s, 'vid' => $v]);

Couldn't find anything suitable to try with EntityTypeManager. TIA.

  • @Lambic, I've removed my duplicate vote. Would you mind posting this as answer?
    – 4uk4
    Commented Dec 1, 2021 at 14:58

1 Answer 1


Glad this question was re-opened, converting my comment to an answer:

If you write it as an entity query then you can cast it to a string, like this:

$t = \Drupal::entityTypeManager()
  ->condition('vid', 'test')
  ->condition('name', 'test'); 
var_export((string) $t)
  • Thanks for sharing the answer! I tried and I get different count of results if I use this piece of code. I guess some additional condition is checked in case of loadByProperties.
    – meen
    Commented Dec 7, 2021 at 13:24
  • Actually, this uses the __toString() magic method behind the scenes. You could call it directly: $query->__toString().
    – Gábor
    Commented Aug 15, 2023 at 14:35
  • Yes but the whole point of magic methods is not to call them directly
    – Lambic
    Commented Sep 10, 2023 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.