12

I'm dealing with some really old drupal project full of SQL queries. PhpStorm's understanding of SQL is very handy however I have problems with drupalism like {table_name}. IDE complains that <reference> expected, got '{'.

How can i set up PhpStorm so that it will understand that the string inside curly braces is just a table name? Or at least make it ignore the curly braces?

  • 1
    Are you using the Drupal integration? I've never seen this issue with PHPStorm. Do you have a special plugin for the SQL that might be causing issues? – googletorp Oct 29 '16 at 12:37
  • @googletorp yes I'm using drupal support plugin bundled with PhpStorm – SiliconMind Oct 29 '16 at 13:45
  • 1
    @SiliconMind That's not supported, I think. You can set sql dialect, but you can't set this specific thing, maybe because other plugins can run your code and give you results in live (like Database Tools and SQL)... maybe. I never used that, but I'm not sure that's a good practice. – golddragon007 Oct 29 '16 at 20:00
9

In PhpStorm Preferences > Tools > Database > User Parameters:

  • Check "Enable in string literals with SQL injection"
  • Add \{\w+\} to Parameter Patterns.

You may also want to modify the line with %\w+ to be allowed for language php for placeholders like %d. See screenshot and also JetBrain's PhpStorm blog post: Database Language Injection Configuration.

PhpStorm preferences screenshot

  • I would change \{\w+\} to \{(\w+)\} to work better with PhpStorm's query execution (the Ctrl+Enter shortcut). Without the extra parentheses, if you try to execute the query, it will prompt you to input a parameter called #1 for your {table_name}, but with the parentheses it will more helpfully prompt your for a table_name parameter. – morbiD Dec 4 '17 at 10:56
  • Further to my previous comment, here are examples of the difference the parentheses would make. For a query string of SELECT * FROM {node} WHERE nid = :nid, pressing the Ctrl+Enter shortcut displays the following prompts: with parentheses and without parentheses. – morbiD Dec 4 '17 at 12:10
  • This doesn't solve the issue where the table columns are marked as unresolved. Need a solution that recognizes the table and its columns, as in the regular sql syntax. – Semra Oct 13 at 12:57
-5

I don't know of any way to make PHPStorm recognize the Drupal SQL syntax.

But if you just want to debug a query that was created by Drupal, then you can remove the curly braces from the query after copying it to the PHPStorm query console.

  • 1
    The reason for the downvotes of this advice is because removing or neglecting to include brackets around table names will interfere with Drupal's table prefixing behavior. See this link for details about the feature: drupal.org/node/2622 – Weston Wedding Sep 18 '17 at 18:58
  • @WestonWedding I did not suggest removing the curly brackets while executing a query in Drupal. My suggestion was to remove the brackets after copying the query from Drupal to the PHPStorm query console. It is a bit of manual work, but helps when debugging queries created by Drupal. – Eyal Oct 2 '17 at 19:15
  • I see! Well, that isn't in your answer anywhere, so some people might be getting confused. – Weston Wedding Oct 2 '17 at 19:52
  • I updated the answer with a little more context. – Eyal Oct 2 '17 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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