📜 ⬆️ ⬇️

Auto-completion of SQL code directly in PHPStorm

PHPStorm has a built-in ability to connect automatic completion of table names and fields in the code editor, but not everyone knows about it.



If your code is displayed like this, then I ask for cat.

Step 1. Connecting the database

Set the cursor in the area of ​​SQL code, I have it highlighted in red, and press Alt + Enter. Choose Configure Data Source
')


In the opened window, select the JDBC driver, in this case MySQL, and, if necessary, click Click here . Select any folder where we want to place the files, and click OK .

After the storm downloads the driver, select the JDBC driver class.
In the Database URL field, select a template and change the settings to yours, also specify the username and password.

If everything is done correctly, after clicking on the Test Connection button, a similar window will appear.



Step 2. Choose SQL Dialects

The SQL code in the editor is still red. We run into the settings.
File - Settings - SQL Dialects
Opposite a folder or a separate file, click on keywords only and select MySQL .
Now the code in the editor has changed color, and even the fields used are probably highlighted, but there is still no autocompletion, or rather it is there, but there are no fields or tables.



Step 3. Add autocompletion

In this case, this step is a matter of taste. You may not want to autocomplete the fields directly in the editor, because you can edit the SQL code separately.
To do this, place the cursor in the area of ​​the SQL code, press Alt + Enter and select something like Edit MySQL fragment . In the open area there will be autocompletion of the fields and tables.

But if you still decide not to stop, then set the cursor in the SQL code, press Alt + Enter and select Language Injection Settings .
In the appeared window we click on php: “SQL select / delete / insert / update / create” and select MySQL .



Everything! Should work. I also advise you to click on the letter A in the lower right corner, if you have one there, and switch to Sort by relevance .

UDP
User Ramzeska suggests that the last step can be done by going to the project settings
“Language Inspections” => “PHP SQL select / delete ...” (in the drop-down menu of the popup window, select MySQL)

Source: https://habr.com/ru/post/184264/


All Articles