Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve SQL table with prefix in PhpStorm?

I'm working on PhpStorm to develop my Prestashop websites and I can't resolve this issue. I work on localhost and successfully connected PhpStorm to my MySQL Server.

Now PhpStorm throws warnings like "unable to resolve table '${_DB_PREFIX_}cms'". Prestashop uses prefixes for table names and it seems PhpStorm can't resolve those tables with prefixes.

Is there a workaround for this ?

Here is a code exemple from Prestashop-1.6 sources :

$sql = 'SELECT c.`id_cms`, cl.`meta_title`, cl.`link_rewrite`
    FROM `'._DB_PREFIX_.'cms` c
    INNER JOIN `'._DB_PREFIX_.'cms_shop` cs
    ON (c.`id_cms` = cs.`id_cms`)
    INNER JOIN `'._DB_PREFIX_.'cms_lang` cl
    ON (c.`id_cms` = cl.`id_cms`)
    WHERE c.`id_cms_category` = '.(int)$id_cms_category.'
    AND cs.`id_shop` = '.(int)$id_shop.'
    AND cl.`id_lang` = '.(int)$id_lang.
    $where_shop.'
    AND c.`active` = 1
    ORDER BY `position`';

Error in PhpStorm for Mysql tables with prefixes

like image 696
Florian Lemaitre Avatar asked Jan 06 '16 10:01

Florian Lemaitre


People also ask

What is a prefix table?

A table prefix is a prefix that is added to every tablename, as the name implies. – Mathias Lykkegaard Lorenzen.

How do I find the table prefix in MySQL?

To list all tables with some prefix, "any number of symbols" wildcard ( % ), should be used. _ is also a wildcard, representing any single symbol, and therefore it should be escaped. Save this answer.


3 Answers

The reason why this isn't work is because you are most likely only loading one schema, you need to load the information_schema.*

To do this, go to the database tab in the top right and where you have added your MySQL database right click and select properties.

Now you'll have a screen called Data Sources and Drivers, it should open on a tab called General, click the third tab called Schemas and and add information_schema.* to this list of loaded Schemas.

Click apply and okay and then PhpStorm will now know your database structure and then be intelligently able to work with you, therefor removing all the errors.

like image 123
Luke Brown Avatar answered Oct 16 '22 06:10

Luke Brown


Edit: As mentioned here, this has been fixed in PhpStorm 2018.2, but only for constants.


I have a solution that doesn't involve throwing your IDE away. :) However, a word of caution: it's an ugly hack™ that comes without guarantees.

  1. Assuming you already have a connection to the db in PhpStorm, generate the ddl for the desired db (Right Click on the connection -> SQL Scripts -> Generate DDL to Clipboard):

    Imgur

  2. Paste the content into some sql file somewhere inside your project. You should probably gitignore this file.

  3. Replace all the tables' prefix in this ddl file with the one from your code. Use the PhpStorm typehint as a guideline. For example '._DB_PREFIX_.'cms would become ${_DB_PREFIX_}cms:

    Imgur

    Note that you may have to use backticks to avoid breaking sql syntax due to curly brackets.

  4. Add the ddl to your phpstorm project:

    Imgur

  5. Everything should now work:

    Imgur

like image 2
Christian Avatar answered Oct 16 '22 07:10

Christian


Add this comment above the $sql query. /** @noinspection SqlResolve */

This will suppress the warning only for this statement.

like image 2
Dilyan Trayanov Avatar answered Oct 16 '22 06:10

Dilyan Trayanov