15

I have only been using PhpStorm a week or so, so far all my SQL queries have been working fine with no errors after setting up the database connection. This current code actually uses a second database (one is for users the other for the specific product) so I added that connection in the database tab too but its still giving me a 'unable to resolve column' warning.

Is there a way to see what database its looking at? Will it work with multiple databases? Or have I done something else wrong?

Error below:

error message

$this->db->setSQL("SELECT T1.*, trunc(sysdate) - trunc(DATE_CHANGED) EXPIRES FROM " . $this->tableName . " T1 WHERE lower(" . $this->primaryKey . ")=lower(:id)")

Also here is what my database settings window looks like as seen some people having problems with parameter patterns causing this error but I'm fairly sure that is not the issue here:

database settings

Using PhpStorm 10.0.3

LazyOne
  • 158,824
  • 45
  • 388
  • 391
Ben Rhys-Lewis
  • 3,118
  • 8
  • 34
  • 45
  • 1
    PhpStorm doesn't know your table name since it's dynamically set in a PHP variable: `$this->tableName` – Álvaro González Apr 22 '16 at 08:33
  • Ah ok. Is there a comment I can use to tell it the tablename? Like using /** @noinspection SqlResolve */ but something more clever. Table name is set at the beginning of the class, can it not see that? Would you just ignore this error or just use the noinspection comment above? – Ben Rhys-Lewis Apr 22 '16 at 08:38
  • What would you guys do in this instance? I have come from sublime text and this is my first IDE! So want to learn a good system and do it 'right'. Is there a comment I can add like the @method used for dynamic methods? – Ben Rhys-Lewis Apr 22 '16 at 13:46

3 Answers3

14

You can set the SQL resolution scope in File -> Settings -> Languages & Frameworks -> SQL Resolution Scopes.

enter image description here

This allows you to provide a default for the entire project and you can optionally define specific mappings to certain paths in the project.

Paul
  • 6,572
  • 2
  • 39
  • 51
  • Thx! All Data Sources not working for me but I selected schema (in postgresql database) and now its working! – feca Feb 18 '18 at 12:32
10

So the short answer is that it cant read the table name as a variable even though its set in a variable above. I thought PhpStorm could work that out. The only way to remove the error would be to either completely turn off SQL inspections (obviously not ideal as I use it throughout my project) or to temporarily disable it for this statement only using the doc comment:

/** @noinspection SqlResolve */

Was hoping to find a more focused comment much like the @var or @method ones to help tell Phpstorm what the table should be so it could still inspect the rest of the statement. Something like: /** @var $this->tableName TABLE_IM_USING */ Maybe in the future JetBrains will add that or make PhpStorm clever enough to look at the variable 3 lines above.

Ben Rhys-Lewis
  • 3,118
  • 8
  • 34
  • 45
2

You can use Nowdoc/Heredoc also instead of using

/** @noinspection SqlResolve */

Here is the example

$name = "your name";
$query = <<<SQL
SELECT * FROM user WHERE name LIKE "%$name%" ORDER BY id
SQL;

Both of the methods will make the warning gone

HendraWD
  • 2,984
  • 2
  • 31
  • 45